<?php
namespace Drupal\paragraphs;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\field\FieldConfigInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\Entity\ParagraphsType;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ParagraphsBehaviorBase extends PluginBase implements ParagraphsBehaviorInterface, ContainerFactoryPluginInterface {
protected $entityFieldManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configuration += $this
->defaultConfiguration();
$this->entityFieldManager = $entity_field_manager;
}
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'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = $configuration + $this
->defaultConfiguration();
}
public function calculateDependencies() {
return [];
}
public function preprocess(&$variables) {
}
public static function isApplicable(ParagraphsType $paragraphs_type) {
return TRUE;
}
public function settingsSummary(Paragraph $paragraph) {
return [];
}
public function settingsIcon(Paragraph $paragraph) {
return [];
}
public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
return $form;
}
public function validateBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
}
public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
$filtered_values = $this
->filterBehaviorFormSubmitValues($paragraph, $form, $form_state);
$paragraph
->setBehaviorSettings($this
->getPluginId(), $filtered_values);
}
protected function filterBehaviorFormSubmitValues(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
$new_array = $form_state
->getValues();
do {
$old_array = $new_array;
$new_array = NestedArray::filter($old_array);
} while ($new_array !== $old_array);
return $new_array;
}
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;
}
}