1

I am working with the Metatags module, and I have these fields.

enter image description here

I need that looks like as in the following screenshot, when the fields meta description and meta keywords are validated.

enter image description here

This is my code, I don't know how can include the class error and put the * symbol. My question is related with styles. I want the field keywords and description looks as fields required for the user.

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

function mymodule_test_validation($form, &$form_state) {
   if (($form_state['values']['metatags']['description']['value'] == '')) {
        form_set_error('Description', t('field is required'));
      } 
      if (($form_state['values']['metatags']['keywords']['value'] == '')) {
        form_set_error('Keywords', t('field is required'));     
      }
}
5
  • Exactly what is not working? Is it that the text isn't red and therefore a styling issue? Is it that the fields are never validated as you want them to be? Are you sure your field names are with the first character capitalized?
    – hampusn
    Commented Sep 13, 2012 at 14:27
  • If a field description or keywords is empty appears the error message, it's doing the validation, I need the styles, the field is in red when the fields are empty and put the * symbol in the label Keyword and Description. Thanks very much.
    – cabita
    Commented Sep 13, 2012 at 14:33
  • I'm sorry, but I still don't understand what you want and what's happening. Can you update your question with the html markup as it is now and also the markup of how you want it to be?
    – hampusn
    Commented Sep 13, 2012 at 14:38
  • I have updated my question. Excuse me for my poor english. Thanks for your help.
    – cabita
    Commented Sep 13, 2012 at 14:45
  • Okey, I understand your issue now. At this date (correct me if I'm wrong) Drupal doesn't print the error messages at the side of every single field. Instead, they are printed in a html unsorted list in page.tpl.php with the variable $messages (search for print $messages; or add it yourself in that template file). A quick search found me an answer here which could probably be modified for your needs. Here is the issue at Drupal.org.
    – hampusn
    Commented Sep 13, 2012 at 14:58

1 Answer 1

-1

If the only validation you are looking for is to make sure the fields are required, then you don't need to call to a validation function. The solution in that case will be to set the "required" attribute to TRUE for the fields description and keywords in the "form_alter" function. This solution will give you the style you are looking for in the required fields.

Something like this (this is a proof of concept):

mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form_id = $form['form_id']['#value'];
  if ($form_id == "article_node_form") {
    // Make META DESCRIPTION required
    $form['metatags']['description'] = array('#require' => TRUE,);

    // Make META KEYWORDS required
    $form['metatags']['keywords'] = array('#require' => TRUE,);
  }
}

To get the right field names do the following:

mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form_id = $form['form_id']['#value'];
  if ($form_id == "article_node_form") {

   print_r($form);

  }
}
3
  • Hi, I tested the option and this code is not working for me. With this code the tab metatags disappears and if I put $form['#metatags']['metatags']['description'] = array('#require' => TRUE,); appears the tab but are not validated the fields. Thanks for your help.
    – cabita
    Commented Sep 13, 2012 at 17:25
  • Hi I used the $form['#metatags']['metatags']['description'] as a proof of concept, I have no idea what is the field name. A solution to get the right field name inside the "if ($form_id == "article_node_form")" add print_r($form); to get the right field name.
    – Emil Orol
    Commented Sep 13, 2012 at 21:56
  • Thanks. I asked in the support of the module metatags, I think is useful in this forum put this thread: drupal.org/node/1782830#comment-6478322
    – cabita
    Commented Sep 14, 2012 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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