3 namespace Drupal\paragraphs;
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;
16 abstract class ParagraphsBehaviorBase extends PluginBase implements ParagraphsBehaviorInterface, ContainerFactoryPluginInterface {
18 use StringTranslationTrait;
21 * The entity field manager.
23 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
25 protected $entityFieldManager;
28 * Constructs a ParagraphsBehaviorBase object.
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.
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;
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')
57 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
64 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { }
69 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { }
74 public function defaultConfiguration() {
81 public function getConfiguration() {
82 return $this->configuration;
88 public function setConfiguration(array $configuration) {
89 $this->configuration = $configuration + $this->defaultConfiguration();
95 public function calculateDependencies() {
102 public function preprocess(&$variables) { }
107 public static function isApplicable(ParagraphsType $paragraphs_type) {
114 public function settingsSummary(Paragraph $paragraph) {
121 public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
128 public function validateBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {}
133 public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
134 $filtered_values = $this->filterBehaviorFormSubmitValues($paragraph, $form, $form_state);
136 $paragraph->setBehaviorSettings($this->getPluginId(), $filtered_values);
140 * Removes default behavior form values before storing the user-set ones.
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.
147 * @param \Drupal\paragraphs\ParagraphInterface $paragraph
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.
155 * An associative array of values submitted to the form with all empty
156 * leaves removed. Subarrays that only contain empty leaves are also
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();
165 $old_array = $new_array;
166 $new_array = NestedArray::filter($old_array);
168 while ($new_array !== $old_array);
176 public function getFieldNameOptions(ParagraphsType $paragraphs_type, $field_type = NULL) {
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();