Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / metatag / src / Form / MetatagDefaultsForm.php
index ccbe7f899b722c6e30e32402a4beb64526b05a52..54e032f06ad813344b1dc610e4e0304b8cb54834 100644 (file)
@@ -4,11 +4,9 @@ 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\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Form\FormStateInterface;
-use Drupal\Core\StringTranslation\TranslatableMarkup;
+use Drupal\metatag\MetatagManager;
 
 /**
  * Class MetatagDefaultsForm.
@@ -28,7 +26,7 @@ class MetatagDefaultsForm extends EntityForm {
     $form['#ajax_wrapper_id'] = 'metatag-defaults-form-ajax-wrapper';
     $ajax = [
       'wrapper' => $form['#ajax_wrapper_id'],
-      'callback' => '::rebuildForm'
+      'callback' => '::rebuildForm',
     ];
     $form['#prefix'] = '<div id="' . $form['#ajax_wrapper_id'] . '">';
     $form['#suffix'] = '</div>';
@@ -56,7 +54,11 @@ class MetatagDefaultsForm extends EntityForm {
         '#options' => $options,
         '#required' => TRUE,
         '#default_value' => $default_type,
-        '#ajax' => $ajax + ['trigger_as' => ['name' => 'select_id_submit']]
+        '#ajax' => $ajax + [
+          'trigger_as' => [
+            'name' => 'select_id_submit',
+          ],
+        ],
       ];
       $form['select_id_submit'] = [
         '#type' => 'submit',
@@ -64,8 +66,8 @@ class MetatagDefaultsForm extends EntityForm {
         '#name' => 'select_id_submit',
         '#ajax' => $ajax,
         '#attributes' => [
-          'class' => ['js-hide']
-        ]
+          'class' => ['js-hide'],
+        ],
       ];
       $values = [];
     }
@@ -94,6 +96,17 @@ class MetatagDefaultsForm extends EntityForm {
     return $form;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  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;
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -143,8 +156,8 @@ class MetatagDefaultsForm extends EntityForm {
     $tag_values = [];
     foreach ($tags as $tag_id => $tag_definition) {
       if ($form_state->hasValue($tag_id)) {
-        // Some plugins need to process form input before storing it.
-        // Hence, we set it and then get it.
+        // Some plugins need to process form input before storing it. Hence, we
+        // set it and then get it.
         $tag = $tag_manager->createInstance($tag_id);
         $tag->setValue($form_state->getValue($tag_id));
         if (!empty($tag->value())) {
@@ -161,6 +174,7 @@ class MetatagDefaultsForm extends EntityForm {
           '%label' => $metatag_defaults->label(),
         ]));
         break;
+
       default:
         drupal_set_message($this->t('Saved the %label Metatag defaults.', [
           '%label' => $metatag_defaults->label(),
@@ -179,9 +193,9 @@ class MetatagDefaultsForm extends EntityForm {
   protected function getAvailableBundles() {
     $options = [];
     $entity_types = $this->getSupportedEntityTypes();
-    /** @var EntityTypeManagerInterface $entity_manager */
+    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager */
     $entity_manager = \Drupal::service('entity_type.manager');
-    /** @var EntityTypeBundleInfoInterface $bundle_info */
+    /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
     $bundle_info = \Drupal::service('entity_type.bundle.info');
     $metatags_defaults_manager = $entity_manager->getStorage('metatag_defaults');
     foreach ($entity_types as $entity_type => $entity_label) {
@@ -205,12 +219,12 @@ class MetatagDefaultsForm extends EntityForm {
    * Returns a list of supported entity types.
    *
    * @return array
-   *  A list of available entity types as $machine_name => $label.
+   *   A list of available entity types as $machine_name => $label.
    */
   protected function getSupportedEntityTypes() {
     $entity_types = [];
 
-    /** @var EntityTypeManagerInterface $entity_manager */
+    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager */
     $entity_manager = \Drupal::service('entity_type.manager');
 
     // A list of entity types that are not supported.
@@ -249,22 +263,24 @@ class MetatagDefaultsForm extends EntityForm {
     return $entity_types;
   }
 
- /**
-  * Returns the text label for the entity type specified.
-  *
-  * @param EntityTypeInterface $entityType
-  *
-  * @return string
-  */
- protected function getEntityTypeLabel(EntityTypeInterface $entityType) {
-   $label = $entityType->getLabel();
-
-   if (is_a($label, 'Drupal\Core\StringTranslation\TranslatableMarkup')) {
-     /** @var TranslatableMarkup $label */
-     $label = $label->render();
-   }
-
-   return $label;
- }
+  /**
+   * Returns the text label for the entity type specified.
+   *
+   * @param Drupal\Core\Entity\EntityTypeInterface $entityType
+   *   The entity type to process.
+   *
+   * @return string
+   *   A label.
+   */
+  protected function getEntityTypeLabel(EntityTypeInterface $entityType) {
+    $label = $entityType->getLabel();
+
+    if (is_a($label, 'Drupal\Core\StringTranslation\TranslatableMarkup')) {
+      /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
+      $label = $label->render();
+    }
+
+    return $label;
+  }
 
 }