diff --git a/src/Plugin/Field/FieldFormatter/MetatagViewsFormatter.php b/src/Plugin/Field/FieldFormatter/MetatagViewsFormatter.php new file mode 100644 index 0000000..f7b95bc --- /dev/null +++ b/src/Plugin/Field/FieldFormatter/MetatagViewsFormatter.php @@ -0,0 +1,157 @@ +get('plugin.manager.metatag.tag'), + $container->get('plugin.manager.metatag.group') + ); + } + + /** + * Constructs a new MetatagFormatter. + * + * @param string $plugin_id + * The plugin_id for the formatter. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The definition of the field to which the formatter is associated. + * @param array $settings + * The formatter settings. + * @param string $label + * The formatter label display setting. + * @param string $view_mode + * The view mode. + * @param array $third_party_settings + * Third party settings. + * @param \Drupal\metatag\MetatagTagPluginManager $metatagTag + * The metatag tag plugin manager service. + * @param \Drupal\metatag\MetatagGroupPluginManager $metatagGroup + * The metatag group plugin manager service. + */ + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, MetatagTagPluginManager $metatagTag, MetatagGroupPluginManager $metatagGroup) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); + $this->metatagTag = $metatagTag; + $this->metatagGroup = $metatagGroup; + } + + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + $settings = []; + + // Fall back to field settings by default. + $settings['field'] = 'title'; + + return $settings; + } + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items, $langcode) { + $entity = $items->getEntity(); + $metatags = metatag_generate_entity_metatags($entity); + $field = $this->getSetting('field'); + + if (!empty($metatags[$field]['#attributes']['content'])) { + $value = $metatags[$field]['#attributes']['content']; + return [['#markup' => Markup::create($value)]]; + } + + // Selected field is not filled, output nothing. + return []; + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $form = parent::settingsForm($form, $form_state); + + $groups = $this->metatagGroup->getDefinitions(); + foreach ($this->metatagTag->getDefinitions() as $id => $tag) { + $gid = (string) $groups[$tag['group']]['label']; + $options[$gid][$tag['id']] = $tag['label']; + } + ksort($options); + foreach ($options as &$optGroup) { + asort($optGroup); + } + + $form['field'] = [ + '#type' => 'select', + '#title' => $this->t('MetaTag field'), + '#default_value' => $this->getSetting('field'), + '#options' => $options, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary = []; + $field = $this->getSetting('field'); + + $summary[] = $this->t('Display: @field field', [ + '@field' => $field, + ]); + + return $summary; + } + +}