Version 1
[yaffs-website] / web / modules / contrib / paragraphs / src / ParagraphsBehaviorBase.php
diff --git a/web/modules/contrib/paragraphs/src/ParagraphsBehaviorBase.php b/web/modules/contrib/paragraphs/src/ParagraphsBehaviorBase.php
new file mode 100644 (file)
index 0000000..f523bc7
--- /dev/null
@@ -0,0 +1,152 @@
+<?php
+
+namespace Drupal\paragraphs;
+
+use Drupal\Component\Plugin\PluginBase;
+use Drupal\Core\Entity\EntityFieldManager;
+use Drupal\Core\Field\FieldConfigInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Drupal\Core\StringTranslation\StringTranslationTrait;
+use Drupal\paragraphs\Entity\Paragraph;
+use Drupal\paragraphs\Entity\ParagraphsType;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+abstract class ParagraphsBehaviorBase extends PluginBase implements ParagraphsBehaviorInterface, ContainerFactoryPluginInterface {
+
+  use StringTranslationTrait;
+
+  /**
+   * The entity field manager.
+   *
+   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
+   */
+  protected $entityFieldManager;
+
+  /**
+   * Constructs a ParagraphsBehaviorBase object.
+   *
+   * @param array $configuration
+   *   A configuration array containing information about the plugin instance.
+   * @param string $plugin_id
+   *   The plugin_id for the plugin instance.
+   * @param mixed $plugin_definition
+   *   The plugin implementation definition.
+   * @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager
+   *   The entity field manager.
+   */
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition);
+    $this->configuration += $this->defaultConfiguration();
+    $this->entityFieldManager = $entity_field_manager;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+    return new static($configuration, $plugin_id, $plugin_definition,
+      $container->get('entity_field.manager')
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
+    return $form;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function defaultConfiguration() {
+    return [];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getConfiguration() {
+    return $this->configuration;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setConfiguration(array $configuration) {
+    $this->configuration = $configuration + $this->defaultConfiguration();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function calculateDependencies() {
+    return [];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function preprocess(&$variables) { }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function isApplicable(ParagraphsType $paragraphs_type) {
+    return TRUE;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function settingsSummary(Paragraph $paragraph) {
+    return [];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
+    return [];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function validateBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {}
+
+  /**
+   * {@inheritdoc}
+   */
+  public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
+    $paragraph->setBehaviorSettings($this->getPluginId(), $form_state->getValues());
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getFieldNameOptions(ParagraphsType $paragraphs_type, $field_type = NULL) {
+    $fields = [];
+    $field_definitions = $this->entityFieldManager->getFieldDefinitions('paragraph', $paragraphs_type->id());
+    foreach ($field_definitions as $name => $definition) {
+      if ($field_definitions[$name] instanceof FieldConfigInterface) {
+        if (empty($field_type) || $definition->getType() == $field_type) {
+          $fields[$name] = $definition->getLabel();
+        }
+      }
+    }
+    return $fields;
+  }
+
+}