View source
<?php
namespace Drupal\metatag\Form;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\metatag\MetatagManager;
use Drupal\metatag\MetatagManagerInterface;
use Drupal\metatag\MetatagTagPluginManager;
use Drupal\metatag\MetatagToken;
use Drupal\page_manager\Entity\PageVariant;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MetatagDefaultsForm extends EntityForm {
protected $entityTypeBundleInfo;
protected $metatagManager;
protected $metatagToken;
protected $moduleHandler;
protected $metatagPluginManager;
public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info, MetatagManagerInterface $metatag_manager, MetatagToken $metatag_token, ModuleHandlerInterface $module_handler, MetatagTagPluginManager $metatag_plugin_manager) {
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->metatagManager = $metatag_manager;
$this->metatagToken = $metatag_token;
$this->moduleHandler = $module_handler;
$this->metatagPluginManager = $metatag_plugin_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.bundle.info'), $container
->get('metatag.manager'), $container
->get('metatag.token'), $container
->get('module_handler'), $container
->get('plugin.manager.metatag.tag'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$metatag_defaults = $this->entity;
$form['#ajax_wrapper_id'] = 'metatag-defaults-form-ajax-wrapper';
$ajax = [
'wrapper' => $form['#ajax_wrapper_id'],
'callback' => '::rebuildForm',
];
$form['#prefix'] = '<div id="' . $form['#ajax_wrapper_id'] . '">';
$form['#suffix'] = '</div>';
$default_type = NULL;
if (!empty($metatag_defaults)) {
$default_type = $metatag_defaults
->getOriginalId();
}
else {
$form_state
->set('default_type', $default_type);
}
$token_types = empty($default_type) ? [] : [
explode('__', $default_type)[0],
];
$form += $this->metatagToken
->tokenBrowser($token_types);
if ($metatag_defaults
->isNew()) {
$options = $this
->getAvailableBundles();
$form['id'] = [
'#type' => 'select',
'#title' => $this
->t('Type'),
'#description' => $this
->t('Select the type of default meta tags you would like to add.'),
'#options' => $options,
'#required' => TRUE,
'#default_value' => $default_type,
'#ajax' => $ajax + [
'trigger_as' => [
'name' => 'select_id_submit',
],
],
];
$form['select_id_submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
'#name' => 'select_id_submit',
'#ajax' => $ajax,
'#attributes' => [
'class' => [
'js-hide',
],
],
];
$values = [];
}
else {
$values = $metatag_defaults
->get('tags');
}
$settings = $this
->config('metatag.settings');
$entity_type_groups = $settings
->get('entity_type_groups');
$metatag_defaults_id = $metatag_defaults
->id();
$type_parts = explode('__', $metatag_defaults_id);
$entity_type = $type_parts[0];
$entity_bundle = isset($type_parts[1]) ? $type_parts[1] : NULL;
$groups = !empty($entity_type_groups[$entity_type]) && !empty($entity_type_groups[$entity_type][$entity_bundle]) ? $entity_type_groups[$entity_type][$entity_bundle] : [];
if (!empty($groups)) {
$form = $this->metatagManager
->form($values, $form, [
$entity_type,
], $groups, NULL, TRUE);
}
else {
$form = $this->metatagManager
->form($values, $form, [], NULL, NULL, TRUE);
}
$form['status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Active'),
'#default_value' => $metatag_defaults
->status(),
];
if ($metatag_defaults_id === 'global') {
$form['status']['#description'] = $this
->t('Warning: disabling the Global default metatag will prevent any metatags from being used.');
}
return $form;
}
public function rebuildForm(array &$form, FormStateInterface $form_state) {
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
if (isset($actions['delete'])) {
$actions['delete']['#access'] = $actions['delete']['#access'] && !in_array($this->entity
->id(), MetatagManager::protectedDefaults());
}
return $actions;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getTriggeringElement()['#name'] == 'select_id_submit') {
$form_state
->set('default_type', $form_state
->getValue('id'));
$form_state
->setRebuild();
}
else {
parent::submitForm($form, $form_state);
}
}
public function save(array $form, FormStateInterface $form_state) {
$metatag_defaults = $this->entity;
$metatag_defaults
->setStatus($form_state
->getValue('status'));
if ($metatag_defaults
->isNew()) {
$metatag_defaults_id = $form_state
->getValue('id');
$type_parts = explode('__', $metatag_defaults_id);
$entity_type = $type_parts[0];
$entity_bundle = isset($type_parts[1]) ? $type_parts[1] : NULL;
$entity_info = $this->entityTypeManager
->getDefinitions();
$entity_label = (string) $entity_info[$entity_type]
->get('label');
if (!is_null($entity_bundle)) {
$bundle_info = $this->entityTypeBundleInfo
->getBundleInfo($entity_type);
if ($entity_type === 'page_variant') {
if ($this->moduleHandler
->moduleExists('metatag_page_manager')) {
$page_variant = PageVariant::load($entity_bundle);
$page = $page_variant
->getPage();
if ($page_variant) {
$entity_label .= ': ' . $page
->label() . ': ' . $page_variant
->label();
}
}
}
else {
$entity_label .= ': ' . $bundle_info[$entity_bundle]['label'];
}
}
$this->entity
->set('label', $entity_label);
}
$tags = $this->metatagPluginManager
->getDefinitions();
$tag_values = [];
foreach ($tags as $tag_id => $tag_definition) {
if ($form_state
->hasValue($tag_id)) {
$tag = $this->metatagPluginManager
->createInstance($tag_id);
$tag
->setValue($form_state
->getValue($tag_id));
if (!empty($tag
->value())) {
$tag_values[$tag_id] = $tag
->value();
}
}
}
$metatag_defaults
->set('tags', $tag_values);
$status = $metatag_defaults
->save();
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addMessage($this
->t('Created the %label Metatag defaults.', [
'%label' => $metatag_defaults
->label(),
]));
break;
default:
$this
->messenger()
->addMessage($this
->t('Saved the %label Metatag defaults.', [
'%label' => $metatag_defaults
->label(),
]));
}
$form_state
->setRedirectUrl($metatag_defaults
->toUrl('collection'));
}
protected function getAvailableBundles() {
$options = [];
$entity_types = static::getSupportedEntityTypes();
$metatags_defaults_manager = $this->entityTypeManager
->getStorage('metatag_defaults');
foreach ($entity_types as $entity_type => $entity_label) {
if (empty($metatags_defaults_manager
->load($entity_type))) {
$options[$entity_label][$entity_type] = "{$entity_label} (Default)";
}
$bundles = $this->entityTypeBundleInfo
->getBundleInfo($entity_type);
foreach ($bundles as $bundle_id => $bundle_metadata) {
$metatag_defaults_id = $entity_type . '__' . $bundle_id;
if (empty($metatags_defaults_manager
->load($metatag_defaults_id))) {
$options[$entity_label][$metatag_defaults_id] = $bundle_metadata['label'];
}
}
}
return $options;
}
public static function getSupportedEntityTypes() {
$entity_types = [];
$entity_type_manager = \Drupal::service('entity_type.manager');
$unsupported_types = [
'block_content',
'comment',
'contact_message',
'menu_link_content',
'shortcut',
];
foreach ($entity_type_manager
->getDefinitions() as $entity_name => $definition) {
if (in_array($entity_name, $unsupported_types)) {
continue;
}
if ($definition instanceof ContentEntityType) {
$links = $definition
->get('links');
if (!empty($links)) {
$entity_types[$entity_name] = static::getEntityTypeLabel($definition);
}
}
}
return $entity_types;
}
public static function getEntityTypeLabel(EntityTypeInterface $entityType) {
$label = $entityType
->getLabel();
if (is_a($label, 'Drupal\\Core\\StringTranslation\\TranslatableMarkup')) {
$label = $label
->render();
}
return $label;
}
}