Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / paragraphs / src / Entity / ParagraphsType.php
index f00f5a32ef77a718185cd1469b3feaf4e2d43c5d..1ebae77b7655d960aca4a56999e85352b64d5d77 100644 (file)
@@ -3,7 +3,9 @@
 namespace Drupal\paragraphs\Entity;
 
 use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
+use Drupal\Core\Entity\EntityStorageInterface;
 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
+use Drupal\Core\Render\Element\File;
 use Drupal\paragraphs\ParagraphsBehaviorCollection;
 use Drupal\paragraphs\ParagraphsBehaviorInterface;
 use Drupal\paragraphs\ParagraphsTypeInterface;
@@ -31,6 +33,8 @@ use Drupal\paragraphs\ParagraphsTypeInterface;
  *   config_export = {
  *     "id",
  *     "label",
+ *     "icon_uuid",
+ *     "description",
  *     "behavior_plugins",
  *   },
  *   bundle_of = "paragraph",
@@ -58,7 +62,21 @@ class ParagraphsType extends ConfigEntityBundleBase implements ParagraphsTypeInt
   public $label;
 
   /**
-   * The paragraphs type behavior plugins configuration keyed by their id.
+   * A brief description of this paragraph type.
+   *
+   * @var string
+   */
+  public $description;
+
+  /**
+   * UUID of the Paragraphs type icon file.
+   *
+   * @var string
+   */
+  protected $icon_uuid;
+
+  /**
+   * The Paragraphs type behavior plugins configuration keyed by their id.
    *
    * @var array
    */
@@ -66,12 +84,23 @@ class ParagraphsType extends ConfigEntityBundleBase implements ParagraphsTypeInt
 
   /**
    * Holds the collection of behavior plugins that are attached to this
-   * paragraphs type.
+   * Paragraphs type.
    *
    * @var \Drupal\paragraphs\ParagraphsBehaviorCollection
    */
   protected $behaviorCollection;
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getIconFile() {
+    if ($this->icon_uuid && $icon = $this->getFileByUuid($this->icon_uuid)) {
+      return $icon;
+    }
+
+    return FALSE;
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -82,6 +111,17 @@ class ParagraphsType extends ConfigEntityBundleBase implements ParagraphsTypeInt
     return $this->behaviorCollection;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getIconUrl() {
+    if ($image = $this->getIconFile()) {
+      return file_create_url($image->getFileUri());
+    }
+
+    return FALSE;
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -89,6 +129,20 @@ class ParagraphsType extends ConfigEntityBundleBase implements ParagraphsTypeInt
     return $this->getBehaviorPlugins()->get($instance_id);
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function calculateDependencies() {
+    parent::calculateDependencies();
+
+    // Add the file icon entity as dependency if a UUID was specified.
+    if ($this->icon_uuid && $file_icon = $this->getIconFile()) {
+      $this->addDependency($file_icon->getConfigDependencyKey(), $file_icon->getConfigDependencyName());
+    }
+
+    return $this->dependencies;
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -103,6 +157,13 @@ class ParagraphsType extends ConfigEntityBundleBase implements ParagraphsTypeInt
     return ['behavior_plugins' => $this->getBehaviorPlugins()];
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getDescription() {
+    return $this->description;
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -117,4 +178,51 @@ class ParagraphsType extends ConfigEntityBundleBase implements ParagraphsTypeInt
     return FALSE;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
+    // Update the file usage for the icon files.
+    if (!$update || $this->icon_uuid != $this->original->icon_uuid) {
+      // The icon has changed. Update file usage.
+      /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
+      $file_usage = \Drupal::service('file.usage');
+
+      // Add usage of the new icon file, if it exists. It might not exist, if
+      // this Paragraphs type was imported as configuration, or if the icon has
+      // just been removed.
+      if ($this->icon_uuid && $new_icon = $this->getFileByUuid($this->icon_uuid)) {
+        $file_usage->add($new_icon, 'paragraphs', 'paragraphs_type', $this->id());
+      }
+      if ($update) {
+        // Delete usage of the old icon file, if it exists.
+        if ($this->original->icon_uuid && $old_icon = $this->getFileByUuid($this->original->icon_uuid)) {
+          $file_usage->delete($old_icon, 'paragraphs', 'paragraphs_type', $this->id());
+        }
+      }
+    }
+
+    parent::postSave($storage, $update);
+  }
+
+  /**
+   * Gets the file entity defined by the UUID.
+   *
+   * @param string $uuid
+   *   The file entity's UUID.
+   *
+   * @return \Drupal\file\FileInterface|null
+   *  The file entity. NULL if the UUID is invalid.
+   */
+  protected function getFileByUuid($uuid) {
+    $files = $this->entityTypeManager()
+      ->getStorage('file')
+      ->loadByProperties(['uuid' => $uuid]);
+    if ($files) {
+      return current($files);
+    }
+
+    return NULL;
+  }
+
 }