Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I would really like to limit the title length on products in Magento.

What I've tried is adding 'maxlength' => 65 somewhere in \app\code\core\Mage\Adminhtml\Block\, without success.

Does someone know how to add this feature? In HTML it will just be adding length="65" maxlength="65".

share|improve this question
    
Slightly off-topic: Better off truncating the title to 12 words, rather than 65 Chars... –  Mike Hudson Jun 28 '12 at 1:29
    
Use the developer block hinting to ensure THAT block is actually making the output you see on front end... or the block that dictates the title's input. –  Chris K Jun 28 '12 at 7:25
    
@MikeHudson True, but HTML doesn't support maxlenght="so many words". –  cobra91 Jun 28 '12 at 14:20
    
@ChrisK In the front-end I'd like to show the same as in the back-end. Truncating isn't an option for me. –  cobra91 Jun 28 '12 at 14:21

4 Answers 4

After almost 10 hours of searching I gave the up the "best" way, and choose for the roundabout.

Simply add

document.getElementById("name").setAttribute("maxlength", "65");
document.getElementById("name").setAttribute("length", "65");

to app/design/adminhtml/default/default/template/catalog/wysiwyg/js.phtml

share|improve this answer
    
Original question should be more like "where do forms come from". The code around the form for new products is in app/design/adminhtml/default/default/template/catalog/product/edit.phtml on line 52: <?php echo $this->getBlockHtml('formkey')?>. Trying to find what actually makes this form's HTML is quite a mystery to me. –  Chris K Jun 28 '12 at 16:59
    
Stumped, I asked this question on Stack Overflow: stackoverflow.com/questions/11249858/… –  Chris K Jun 28 '12 at 17:27

Copy the app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Attributes.php

to

app/code/local/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Attributes.php

In the function _prepareForm(),

after the line if ($form->getElement('meta_description')) { ...

Add

if ($form->getElement('name')) {
    $form->getElement('name')->setOnkeyup('checkMaxLength(this, 1500);');
}

This should work

share|improve this answer

As a result from that SO question. One repeating pattern in form creation is $fieldset->addField, so that presented itself as a key way to grep in files.

user@magento:~/www/app$ grep -rin "addField.*text" * | grep -i product
code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Config/Simple.php:140:        $fieldset->addField('simple_product_inventory_qty', 'text', array(

There were about a dozen results which were quickly narrowed down (we're not caring about giftcard or Attribute sets). I'm not 100% sure THIS file is the answer but it seems that some logic could be added to catch whether or not the input's name=="Name" and then a maxlength could then be added in.

share|improve this answer

JS or POST trunc is the best bet, but an alternate failproof method is at the DB. You can limit it in DB by changing the cell structure to varchar(65) which would only allow 65 characters to be stored there. In phpMyAdmin you find the table, click it, then look for the "structure" tab. Find the col you wanna limit, pick varchar from dropdown, then set it to 65. This will also trunc all existing titles to 65.

EDIT: This script may be helpful too if you just wanna count the chars instead and leave it to your editors to keep it around 65. This would work on any field, just change the input[name=""] to whatever field you need counted:

<script type="text/javascript">
    var count_name = $('input[name="MY-TITLE-INPUT-NAME"]');
    var count_name_val = $(count_name).val().length;
    var stage_65 = '<br/><div style="font-style:italic;">&nbsp;&nbsp;Using <span class="stage_65">' + count_name_val + '</span> of 65 Google Chars</div>';

    $(count_name).after(stage_65).on('keyup', function(){
        $('.stage_65').html($(this).val().length);
    });
</script>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.