Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / block_content / src / Entity / BlockContent.php
index 3c8858ff88d2fb88585161675c9556ce6554a353..1037beeef52cf8e67a63e427f9c6764a9ef4de0b 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Drupal\block_content\Entity;
 
+use Drupal\block_content\Access\RefinableDependentAccessTrait;
 use Drupal\Core\Entity\EditorialContentEntityBase;
 use Drupal\Core\Entity\EntityStorageInterface;
 use Drupal\Core\Entity\EntityTypeInterface;
@@ -15,6 +16,13 @@ use Drupal\user\UserInterface;
  * @ContentEntityType(
  *   id = "block_content",
  *   label = @Translation("Custom block"),
+ *   label_collection = @Translation("Custom blocks"),
+ *   label_singular = @Translation("custom block"),
+ *   label_plural = @Translation("custom blocks"),
+ *   label_count = @PluralTranslation(
+ *     singular = "@count custom block",
+ *     plural = "@count custom blocks",
+ *   ),
  *   bundle_label = @Translation("Custom block type"),
  *   handlers = {
  *     "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
@@ -70,6 +78,8 @@ use Drupal\user\UserInterface;
  */
 class BlockContent extends EditorialContentEntityBase implements BlockContentInterface {
 
+  use RefinableDependentAccessTrait;
+
   /**
    * The theme the block is being created in.
    *
@@ -111,7 +121,9 @@ class BlockContent extends EditorialContentEntityBase implements BlockContentInt
    */
   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
     parent::postSave($storage, $update);
-    static::invalidateBlockPluginCache();
+    if ($this->isReusable() || (isset($this->original) && $this->original->isReusable())) {
+      static::invalidateBlockPluginCache();
+    }
   }
 
   /**
@@ -119,7 +131,14 @@ class BlockContent extends EditorialContentEntityBase implements BlockContentInt
    */
   public static function postDelete(EntityStorageInterface $storage, array $entities) {
     parent::postDelete($storage, $entities);
-    static::invalidateBlockPluginCache();
+    /** @var \Drupal\block_content\BlockContentInterface $block */
+    foreach ($entities as $block) {
+      if ($block->isReusable()) {
+        // If any deleted blocks are reusable clear the block cache.
+        static::invalidateBlockPluginCache();
+        return;
+      }
+    }
   }
 
   /**
@@ -193,6 +212,13 @@ class BlockContent extends EditorialContentEntityBase implements BlockContentInt
       ->setTranslatable(TRUE)
       ->setRevisionable(TRUE);
 
+    $fields['reusable'] = BaseFieldDefinition::create('boolean')
+      ->setLabel(t('Reusable'))
+      ->setDescription(t('A boolean indicating whether this block is reusable.'))
+      ->setTranslatable(FALSE)
+      ->setRevisionable(FALSE)
+      ->setDefaultValue(TRUE);
+
     return $fields;
   }
 
@@ -275,6 +301,27 @@ class BlockContent extends EditorialContentEntityBase implements BlockContentInt
     return $this;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function isReusable() {
+    return (bool) $this->get('reusable')->value;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setReusable() {
+    return $this->set('reusable', TRUE);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setNonReusable() {
+    return $this->set('reusable', FALSE);
+  }
+
   /**
    * Invalidates the block plugin cache after changes and deletions.
    */