View source  
  <?php
namespace Drupal\metatag\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\metatag\MetatagManagerInterface;
use Drupal\metatag\MetatagTagPluginManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class MetatagFirehose extends WidgetBase implements ContainerFactoryPluginInterface {
  use StringTranslationTrait;
  
  protected $metatagManager;
  
  protected $metatagPluginManager;
  
  protected $configFactory;
  
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
      ->get('metatag.manager'), $container
      ->get('plugin.manager.metatag.tag'), $container
      ->get('config.factory'));
  }
  
  public static function defaultSettings() {
    return [
      'sidebar' => TRUE,
    ] + parent::defaultSettings();
  }
  
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element['sidebar'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Place field in sidebar'),
      '#default_value' => $this
        ->getSetting('sidebar'),
      '#description' => $this
        ->t('If checked, the field will be placed in the sidebar on entity forms.'),
    ];
    return $element;
  }
  
  public function settingsSummary() {
    if ($this
      ->getSetting('sidebar')) {
      $summary[] = $this
        ->t('Use sidebar: Yes');
    }
    else {
      $summary[] = $this
        ->t('Use sidebar: No');
    }
    return $summary;
  }
  
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, MetatagManagerInterface $manager, MetatagTagPluginManager $plugin_manager, ConfigFactoryInterface $config_factory) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
    $this->metatagManager = $manager;
    $this->metatagPluginManager = $plugin_manager;
    $this->configFactory = $config_factory;
  }
  
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $item = $items[$delta];
    $default_tags = metatag_get_default_tags($items
      ->getEntity());
    
    $values = [];
    if (!empty($item->value)) {
      $values = unserialize($item->value);
    }
    
    if (!empty($default_tags)) {
      foreach ($default_tags as $tag_id => $tag_value) {
        if (!isset($values[$tag_id]) && !empty($tag_value)) {
          $values[$tag_id] = $tag_value;
        }
      }
    }
    
    $settings = $this->configFactory
      ->get('metatag.settings');
    $entity_type_groups = $settings
      ->get('entity_type_groups');
    
    $entity_type = $item
      ->getEntity()
      ->getentityTypeId();
    $entity_bundle = $item
      ->getEntity()
      ->bundle();
    
    $groups = [];
    if (!empty($entity_type_groups[$entity_type]) && !empty($entity_type_groups[$entity_type][$entity_bundle])) {
      $groups = $entity_type_groups[$entity_type][$entity_bundle];
    }
    
    if (!empty($groups)) {
      $element = $this->metatagManager
        ->form($values, $element, [
        $entity_type,
      ], $groups);
    }
    else {
      $element = $this->metatagManager
        ->form($values, $element, [
        $entity_type,
      ]);
    }
    
    $sidebar = $this
      ->getSetting('sidebar');
    if ($sidebar) {
      $element['#group'] = 'advanced';
    }
    return $element;
  }
  
  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
    
    $tag_manager = $this->metatagPluginManager;
    foreach ($values as &$value) {
      $flattened_value = [];
      foreach ($value as $group) {
        
        if (is_array($group)) {
          foreach ($group as $tag_id => $tag_value) {
            $tag = $tag_manager
              ->createInstance($tag_id);
            $tag
              ->setValue($tag_value);
            if (!empty($tag
              ->value())) {
              $flattened_value[$tag_id] = $tag
                ->value();
            }
          }
        }
      }
      $value = serialize($flattened_value);
    }
    return $values;
  }
}