2

I have a content type and I am interested in make required the field keyword and field description of the metatags module? anyone knows how can I do it?

The field title is not required. The field keywords is required The field metatags is required.

I am working on drupal 7.

Thanks a lot!!

I was trying this code:

function mymodule_form_alter(&$form, &$form_state) {
    if ($form['#form_id'] = 'article_node_form') {
        $form['#validate'][] = 'mymodule_form_validate_fields_metatags';
    }
}

function mymodule_validate_fields_metatags ($form, &$form_state) {
     if (($form['#metatags']['metatags']['description']['value']) == '') {
        form_set_error('Description', t('The field Description is required'));
      } 
      if (($form['#metatags']['metatags']['keywords']['value']) == '') {
        form_set_error('', t('The field Keywords is required'));        
      }  

 }

this code generate an error : NOtice: undefined index.

I have tried this code and not print dsm:

mymodule_form_alter(&$form, &$form_state) {
    if ($form['#form_id'] = 'article_node_form') {
        $form['#validate'][] = 'mymodule_form_validate_fields_metatags';
    }
}

function mymodule_form_validate_fields_metatags ($form, &$form_state) {
dsm("hello");  

 }

if no print hello how can test the form_state? I don't understand what is my error. Thanks for your help in advance.

2 Answers 2

1

You can use code similar to the following in a custom module/theme:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['metatags'])) {
    if (isset($form['metatags']['description']['value'])) {
      $form['metatags']['description']['value']['#required'] = TRUE;
    }
    if (isset($form['metatags']['keywords']['value'])) {
      $form['metatags']['keywords']['value']['#required'] = TRUE;
    }
  }
}
6
  • Hi, thanks for your answer. Your code doesn't work for me. Thanks.
    – cabita
    Sep 12, 2012 at 23:31
  • The code in your example would probably work if it was more like if ($form_state['values']['metatags']['description']['value'] == '') {...
    – Clive
    Sep 13, 2012 at 0:14
  • Thanks. I changed the code and nothing. I tested with this code and not print the dsm, what is my error? function mymodule_form_alter(&$form, &$form_state) { if ($form['#form_id'] = 'article_node_form') { $form['#validate'][] = 'mymodule_form_validate_fields_metatags'; } } function mymodule_form_validate_fields_metatags ($form, &$form_state) { dsm("hello"); }
    – cabita
    Sep 13, 2012 at 2:08
  • I'm going to update your answer with the code worked for me. Thanks.
    – cabita
    Sep 13, 2012 at 11:09
  • Do you know how can I put the class error? I tried this: $form_state['values']['metatags']['keywords']['value'][#prefix] = '<div class="error">'; $form_state['values']['metatags']['keywords']['value'][#suffix] = '</div>'; and nothing. Thanks.
    – cabita
    Sep 13, 2012 at 11:38
1

Maybe not a nice solution, but it's possible without any custom code:

  1. add a "normal" field for keywords and a "normal" field for description, set both to required
  2. hide both fields in 'Manage display'
  3. edit the 'Meta tags' settings to give a default value for Meta keywords and Meta description
  4. set the tokens of the two normal fields as default values

To the editing user you only show the two normal fields. So don't give them right permission to change the Meta tags fields directly, they should only be able to use the two surrogate fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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