f00f5a32ef77a718185cd1469b3feaf4e2d43c5d
[yaffs-website] / web / modules / contrib / paragraphs / src / Entity / ParagraphsType.php
1 <?php
2
3 namespace Drupal\paragraphs\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
6 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
7 use Drupal\paragraphs\ParagraphsBehaviorCollection;
8 use Drupal\paragraphs\ParagraphsBehaviorInterface;
9 use Drupal\paragraphs\ParagraphsTypeInterface;
10
11 /**
12  * Defines the ParagraphsType entity.
13  *
14  * @ConfigEntityType(
15  *   id = "paragraphs_type",
16  *   label = @Translation("Paragraphs type"),
17  *   handlers = {
18  *     "list_builder" = "Drupal\paragraphs\Controller\ParagraphsTypeListBuilder",
19  *     "form" = {
20  *       "add" = "Drupal\paragraphs\Form\ParagraphsTypeForm",
21  *       "edit" = "Drupal\paragraphs\Form\ParagraphsTypeForm",
22  *       "delete" = "Drupal\paragraphs\Form\ParagraphsTypeDeleteConfirm"
23  *     }
24  *   },
25  *   config_prefix = "paragraphs_type",
26  *   admin_permission = "administer paragraphs types",
27  *   entity_keys = {
28  *     "id" = "id",
29  *     "label" = "label",
30  *   },
31  *   config_export = {
32  *     "id",
33  *     "label",
34  *     "behavior_plugins",
35  *   },
36  *   bundle_of = "paragraph",
37  *   links = {
38  *     "edit-form" = "/admin/structure/paragraphs_type/{paragraphs_type}",
39  *     "delete-form" = "/admin/structure/paragraphs_type/{paragraphs_type}/delete",
40  *     "collection" = "/admin/structure/paragraphs_type",
41  *   }
42  * )
43  */
44 class ParagraphsType extends ConfigEntityBundleBase implements ParagraphsTypeInterface, EntityWithPluginCollectionInterface {
45
46   /**
47    * The ParagraphsType ID.
48    *
49    * @var string
50    */
51   public $id;
52
53   /**
54    * The ParagraphsType label.
55    *
56    * @var string
57    */
58   public $label;
59
60   /**
61    * The paragraphs type behavior plugins configuration keyed by their id.
62    *
63    * @var array
64    */
65   public $behavior_plugins = [];
66
67   /**
68    * Holds the collection of behavior plugins that are attached to this
69    * paragraphs type.
70    *
71    * @var \Drupal\paragraphs\ParagraphsBehaviorCollection
72    */
73   protected $behaviorCollection;
74
75   /**
76    * {@inheritdoc}
77    */
78   public function getBehaviorPlugins() {
79     if (!isset($this->behaviorCollection)) {
80       $this->behaviorCollection = new ParagraphsBehaviorCollection(\Drupal::service('plugin.manager.paragraphs.behavior'), $this->behavior_plugins);
81     }
82     return $this->behaviorCollection;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function getBehaviorPlugin($instance_id) {
89     return $this->getBehaviorPlugins()->get($instance_id);
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function getEnabledBehaviorPlugins() {
96     return $this->getBehaviorPlugins()->getEnabled();
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function getPluginCollections() {
103     return ['behavior_plugins' => $this->getBehaviorPlugins()];
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function hasEnabledBehaviorPlugin($plugin_id) {
110     $plugins = $this->getBehaviorPlugins();
111     if ($plugins->has($plugin_id)) {
112       /** @var ParagraphsBehaviorInterface $plugin */
113       $plugin = $plugins->get($plugin_id);
114       $config = $plugin->getConfiguration();
115       return (array_key_exists('enabled', $config) && $config['enabled'] === TRUE);
116     }
117     return FALSE;
118   }
119
120 }