Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
DamienMcKennaTry hook_form_alter?
Comment #2
cabita CreditAttribution: cabita commentedHi,
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.
Comment #3
DamienMcKennaOk, 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.
Comment #4
cabita CreditAttribution: cabita commentedThanks. I look forward your answer.
Comment #5
DamienMcKennaDuplicate of #1391968: Document how to disable/modify meta tags.
Comment #6
fietserwinAs 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:
Comment #7
remkovdz CreditAttribution: remkovdz commentedCan anybody help with the code to make the description field mandatory? I'm not a developer but really need the function. :( Thanks!