Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / menu_link_content / src / Entity / MenuLinkContent.php
index 460c0df0d4c7ff14ecb6b643f592b8424b91ce43..0dfe7cb435e49c7420dac08dad7dba0889988770 100644 (file)
@@ -4,6 +4,7 @@ namespace Drupal\menu_link_content\Entity;
 
 use Drupal\Core\Entity\ContentEntityBase;
 use Drupal\Core\Entity\EntityChangedTrait;
+use Drupal\Core\Entity\EntityPublishedTrait;
 use Drupal\Core\Entity\EntityStorageInterface;
 use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Field\BaseFieldDefinition;
@@ -19,6 +20,13 @@ use Drupal\menu_link_content\MenuLinkContentInterface;
  * @ContentEntityType(
  *   id = "menu_link_content",
  *   label = @Translation("Custom menu link"),
+ *   label_collection = @Translation("Custom menu links"),
+ *   label_singular = @Translation("custom menu link"),
+ *   label_plural = @Translation("custom menu links"),
+ *   label_count = @PluralTranslation(
+ *     singular = "@count custom menu link",
+ *     plural = "@count custom menu links",
+ *   ),
  *   handlers = {
  *     "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
  *     "storage_schema" = "Drupal\menu_link_content\MenuLinkContentStorageSchema",
@@ -37,7 +45,8 @@ use Drupal\menu_link_content\MenuLinkContentInterface;
  *     "label" = "title",
  *     "langcode" = "langcode",
  *     "uuid" = "uuid",
- *     "bundle" = "bundle"
+ *     "bundle" = "bundle",
+ *     "published" = "enabled",
  *   },
  *   links = {
  *     "canonical" = "/admin/structure/menu/item/{menu_link_content}/edit",
@@ -49,6 +58,7 @@ use Drupal\menu_link_content\MenuLinkContentInterface;
 class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterface {
 
   use EntityChangedTrait;
+  use EntityPublishedTrait;
 
   /**
    * A flag for whether this entity is wrapped in a plugin instance.
@@ -186,6 +196,7 @@ class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterf
       $this->setRequiresRediscovery(FALSE);
     }
   }
+
   /**
    * {@inheritdoc}
    */
@@ -228,6 +239,14 @@ class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterf
     foreach ($entities as $menu_link) {
       /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_link */
       $menu_link_manager->removeDefinition($menu_link->getPluginId(), FALSE);
+
+      // Children get re-attached to the menu link's parent.
+      $parent_plugin_id = $menu_link->getParentId();
+      $children = $storage->loadByProperties(['parent' => $menu_link->getPluginId()]);
+      foreach ($children as $child) {
+        /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $child */
+        $child->set('parent', $parent_plugin_id)->save();
+      }
     }
   }
 
@@ -238,6 +257,9 @@ class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterf
     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
     $fields = parent::baseFieldDefinitions($entity_type);
 
+    // Add the publishing status field.
+    $fields += static::publishedBaseFieldDefinitions($entity_type);
+
     $fields['id']->setLabel(t('Entity ID'))
       ->setDescription(t('The entity ID for this menu link content entity.'));
 
@@ -338,19 +360,21 @@ class MenuLinkContent extends ContentEntityBase implements MenuLinkContentInterf
         'weight' => 0,
       ]);
 
-    $fields['enabled'] = BaseFieldDefinition::create('boolean')
-      ->setLabel(t('Enabled'))
-      ->setDescription(t('A flag for whether the link should be enabled in menus or hidden.'))
-      ->setDefaultValue(TRUE)
-      ->setDisplayOptions('view', [
-        'label' => 'hidden',
-        'type' => 'boolean',
-        'weight' => 0,
-      ])
-      ->setDisplayOptions('form', [
-        'settings' => ['display_label' => TRUE],
-        'weight' => -1,
-      ]);
+    // Override some properties of the published field added by
+    // \Drupal\Core\Entity\EntityPublishedTrait::publishedBaseFieldDefinitions().
+    $fields['enabled']->setLabel(t('Enabled'));
+    $fields['enabled']->setDescription(t('A flag for whether the link should be enabled in menus or hidden.'));
+    $fields['enabled']->setTranslatable(FALSE);
+    $fields['enabled']->setRevisionable(FALSE);
+    $fields['enabled']->setDisplayOptions('view', [
+      'label' => 'hidden',
+      'type' => 'boolean',
+      'weight' => 0,
+    ]);
+    $fields['enabled']->setDisplayOptions('form', [
+      'settings' => ['display_label' => TRUE],
+      'weight' => -1,
+    ]);
 
     $fields['parent'] = BaseFieldDefinition::create('string')
       ->setLabel(t('Parent plugin ID'))