Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / paragraphs / src / ParagraphsBehaviorBase.php
1 <?php
2
3 namespace Drupal\paragraphs;
4
5 use Drupal\Component\Plugin\PluginBase;
6 use Drupal\Component\Utility\NestedArray;
7 use Drupal\Core\Entity\EntityFieldManager;
8 use Drupal\Core\Field\FieldConfigInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Drupal\Core\StringTranslation\StringTranslationTrait;
12 use Drupal\paragraphs\Entity\Paragraph;
13 use Drupal\paragraphs\Entity\ParagraphsType;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 abstract class ParagraphsBehaviorBase extends PluginBase implements ParagraphsBehaviorInterface, ContainerFactoryPluginInterface {
17
18   use StringTranslationTrait;
19
20   /**
21    * The entity field manager.
22    *
23    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
24    */
25   protected $entityFieldManager;
26
27   /**
28    * Constructs a ParagraphsBehaviorBase object.
29    *
30    * @param array $configuration
31    *   A configuration array containing information about the plugin instance.
32    * @param string $plugin_id
33    *   The plugin_id for the plugin instance.
34    * @param mixed $plugin_definition
35    *   The plugin implementation definition.
36    * @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager
37    *   The entity field manager.
38    */
39   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager) {
40     parent::__construct($configuration, $plugin_id, $plugin_definition);
41     $this->configuration += $this->defaultConfiguration();
42     $this->entityFieldManager = $entity_field_manager;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
49     return new static($configuration, $plugin_id, $plugin_definition,
50       $container->get('entity_field.manager')
51     );
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
58     return $form;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function defaultConfiguration() {
75     return [];
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function getConfiguration() {
82     return $this->configuration;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function setConfiguration(array $configuration) {
89     $this->configuration = $configuration + $this->defaultConfiguration();
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function calculateDependencies() {
96     return [];
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function preprocess(&$variables) { }
103
104   /**
105    * {@inheritdoc}
106    */
107   public static function isApplicable(ParagraphsType $paragraphs_type) {
108     return TRUE;
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function settingsSummary(Paragraph $paragraph) {
115     return [];
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
122     return $form;
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function validateBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {}
129
130   /**
131    * {@inheritdoc}
132    */
133   public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
134     $filtered_values = $this->filterBehaviorFormSubmitValues($paragraph, $form, $form_state);
135
136     $paragraph->setBehaviorSettings($this->getPluginId(), $filtered_values);
137   }
138
139   /**
140    * Removes default behavior form values before storing the user-set ones.
141    *
142    * Default implementation considers a value to be default if and only if it is
143    * an empty value. Behavior plugins that do not consider all empty values to
144    * be default should override this method or
145    * \Drupal\paragraphs\ParagraphsBehaviorBase::submitBehaviorForm.
146    *
147    * @param \Drupal\paragraphs\ParagraphInterface $paragraph
148    *   The paragraph.
149    * @param array $form
150    *   An associative array containing the initial structure of the plugin form.
151    * @param \Drupal\Core\Form\FormStateInterface $form_state
152    *   The current state of the form.
153    *
154    * @return array
155    *   An associative array of values submitted to the form with all empty
156    *   leaves removed. Subarrays that only contain empty leaves are also
157    *   removed.
158    */
159   protected function filterBehaviorFormSubmitValues(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
160     // Keeps removing empty leaves, until there are none left. So if a subarray
161     // only contains empty leaves, that subarray itself will be removed.
162     $new_array = $form_state->getValues();
163
164     do {
165       $old_array = $new_array;
166       $new_array = NestedArray::filter($old_array);
167     }
168     while ($new_array !== $old_array);
169
170     return $new_array;
171   }
172
173   /**
174    * {@inheritdoc}
175    */
176   public function getFieldNameOptions(ParagraphsType $paragraphs_type, $field_type = NULL) {
177     $fields = [];
178     $field_definitions = $this->entityFieldManager->getFieldDefinitions('paragraph', $paragraphs_type->id());
179     foreach ($field_definitions as $name => $definition) {
180       if ($field_definitions[$name] instanceof FieldConfigInterface) {
181         if (empty($field_type) || $definition->getType() == $field_type) {
182           $fields[$name] = $definition->getLabel();
183         }
184       }
185     }
186     return $fields;
187   }
188
189 }