Hi.

I am working with the meta tags module and I need the field Keywords and field Description be required or mandatory and the field Title be optional in a specific content type. How can I do it?

Thanks.

Comments

Status:Active» Fixed

Hi,

I found to do the fields mandatory or required. Now I have troubles for put styles. I need that the fields Keywords and Description look like as : http://screencast.com/t/cpbs2EJAgZD

I don't know how can I put the * symbol to the labels "Keywords" and "Description" and I don't know how can I do for the red border only is around the field of the box.

The code worked is the follow and validate the fields without styles:

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'));
}
}

Also I tried this option but doesn't work:

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,);
}
}

If I put the last code, the tab metatags disappears, if I add

$form['#metatags']['metatags']['description'] = array('#require' => TRUE,);
instead of
$form['metatags']['description'] = array('#require' => TRUE,);

the tab metatags appears but the fields are not validated.

Thanks for your help.

Status:Fixed» Active

Ok, so editing Metatag's fields is not as simple as I alluded to, you can't just hook_form_alter() it because of how the fields are inserted. I'll have to get back to you on the best way of doing it.

Thanks. I look forward your answer.

Status:Active» Closed (duplicate)

As the issue turned into a documentation issue, I want to point out some flaws in the code of #2 as well as possible optimizations/generalizations:

  • If you are only doing it for a specific (type of) form, it is better to use use hook_form_FORM_ID_alter or hook_form_BASE_FORM_ID_alter.
  • If you want to do it for all forms where metatags also joins in, you might also just check on the presence of $form['metatags'].
  • The validation should be placed on the metatags element as an #element_validate, this makes it easier to set the error on the metatags element. The current coe won't work as it does not properly specify the element to set the error on.
  • The #require entry overwrites the complete element instead of merging/adding it to the existing element.
  • A required validation is checked by the core form handler and does not have to be programmed in custom code.

Can anybody help with the code to make the description field mandatory? I'm not a developer but really need the function. :( Thanks!