Home / Blog / Altering Metatag Global Form in Drupal 8
Angel's picture
Angel Alvarado R.
Soft. and Data engineer.
23 Jan 2017
Altering
Metatag Global Form in Drupal 8

Drupal 8 Altering Metatag

Body: 

In order to alter the keywords textfield in the metatag module I used the hook hook_form_alter. Here's the form after being altered
 
Finding the form ID: 
 
This is easy using Devel module and its webprofiler submodule. Once they are enabled go to admin/config/search/metatag/global. Then hover the ‘form’ icon.
 
 
Code
 
/**
 * Implements hook_form_alter().
 *
 * Altering the form with our custom module.
 * This is another option hook_form_metatag_defaults_edit_form_alter
 */
function schedule_form_alter(&$formFormStateInterface $form_state$form_id) {
  if ($form_id == 'metatag_defaults_edit_form') {
    $form['basic']['keywords']['#type'] = 'textarea';
    $form['basic']['keywords']['#rows'] = 3;
    $form['basic']['keywords']['#maxlength'] = 768;
  }
}
 
Troubleshooting:
  • I was using Drupal 8.1.8 and wanted to keep using Drupal Console to clear cache. It did not work because I was using Drupal console 0.10.13. To update Drupal Console
    • $ drupalconsole —version: "Drupal Console version 0.10.13"
    • $ curl https://drupalconsole.com/installer -L -o drupal.phar
    • $ mv drupal.phar /usr/local/bin/drupalconsole
    • $ chmod +x  /usr/local/bin/drupalconsole
    • drupalconsole —version: "Drupal Console" version "1.0.0-beta5"
  • While trying to use Drupal Console 1.0.0-beta5 I found that it had some issues with the module ‘Metatag’ version beta 9: https://www.drupal.org/node/2752239#comment-11428453. I updated the Metatag module to the dev version https://www.drupal.org/project/metatag/releases/8.x-1.x-dev and was able to use Drupal Console again.
    • $ cd ..drupal8projectfolder
    • $ drupalconsole cr all
 
References: 
 
@Todo
 
     Manage metadata block configuration via Configuration Management: https://www.lullabot.com/articles/configuration-management-in-drupal-8-the-key-concepts