Remove Extra Metatags on Node Form, Drupal 8

 

Personally, I feel overwhelmed looking at the ocean of metatags presented on the node edit page when the metatag module is installed.

How much more overwhelmed would a regular ol' user feel?

I created a small modules to hide the extra form elements from the metatag module:

metatag_limit.module:

<?php
use \Drupal\Core\Render\Element;
 
// Add metatag key/value categories here to be preserved.
const ALLOWED_METATAGS = array(
  'basic' => array('title', 'description'),
  // 'advanced' => array(),
  'open_graph' => array('og_title', 'og_description', 'og_image'),
  'twitter_cards' => array('twitter_cards_description', 'twitter_cards_title', 'twitter_cards_image')
);
 
function limit_metatags_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if (!empty($form['field_meta_tags']['widget'][0])) {
    $el =& $form['field_meta_tags']['widget'][0];
    foreach (Element::children($el) as $tag_type_name) {
      if (!isset(ALLOWED_METATAGS[$tag_type_name])) {
        $el[$tag_type_name]['#access'] = FALSE;
        continue;
      }
      if (isset($el[$tag_type_name])) {
        foreach (Element::children($el[$tag_type_name]) as $tag_name) {
          if (!in_array($tag_name, ALLOWED_METATAGS[$tag_type_name])) {
            $el[$tag_type_name][$tag_name]['#access'] = FALSE;
          }
        }
      }
    }
  }
}

metatag_limit.info.yml:

name: Limit Metatags
type: module
description: 'Limit the number of metatags shown to editors'
core: 8.x
 
dependencies:
  - metatag

About the Author

Hi. My name is Jeremiah John. I'm a sf/f writer and activist.

I just completed a dystopian science fiction novel. I run a website which I created that connects farms with churches, mosques, and synagogues to buy fresh vegetables directly and distribute them on a sliding scale to those in need.

In 2003, I spent six months in prison for civil disobedience while working to close the School of the Americas, converting to Christianity, as one does, while I was in the clink.