3 namespace Drupal\paragraphs\Feeds\Target;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\feeds\Feeds\Target\Text;
10 use Drupal\feeds\Plugin\Type\Target\ConfigurableTargetInterface;
13 * Feeds target plugin for Paragraphs fields.
17 * field_types = {"entity_reference_revisions"},
18 * arguments = {"@entity.manager", "@current_user"}
21 class Paragraphs extends Text implements ConfigurableTargetInterface {
24 * The paragraph storage.
26 * @var \Drupal\Core\Entity\EntityStorageInterface
28 protected $paragraphStorage;
31 * The paragraphs type storage.
33 * @var \Drupal\Core\Entity\EntityStorageInterface
35 protected $paragraphsTypeStorage;
38 * The field config storage.
40 * @var \Drupal\Core\Entity\EntityStorageInterface
42 protected $fieldConfigStorage;
45 * Constructs the target plugin.
47 public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user) {
48 parent::__construct($configuration, $plugin_id, $plugin_definition, $current_user);
49 $this->paragraphStorage = $entity_type_manager->getStorage('paragraph');
50 $this->paragraphsTypeStorage = $entity_type_manager->getStorage('paragraphs_type');
51 $this->fieldConfigStorage = $entity_type_manager->getStorage('field_config');
57 public function defaultConfiguration() {
58 return parent::defaultConfiguration() + [
59 'paragraphs_type' => NULL,
60 'paragraph_field' => NULL,
67 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
68 $form['paragraphs_type'] = [
70 '#title' => $this->t('Paragraphs type'),
72 '#options' => array_map(function(EntityInterface $paragraphs_type) {
73 return $paragraphs_type->label();
74 }, $this->paragraphsTypeStorage->loadMultiple()),
75 '#default_value' => $this->configuration['paragraphs_type'],
78 // Load and filter field configs to create options.
79 /** @var \Drupal\field\FieldConfigInterface[] $field_configs */
80 $field_configs = $this->fieldConfigStorage->loadByProperties([
81 'entity_type' => 'paragraph',
82 'bundle' => $this->configuration['paragraphs_type'],
85 foreach ($field_configs as $field_config) {
86 if (in_array($field_config->getType(), ['text', 'text_long', 'text_with_summary'])) {
87 $field_options[$field_config->getName()] = $field_config->label();
91 $form['paragraph_field'] = [
93 '#title' => $this->t('Paragraph field'),
94 '#description' => $this->t('<strong>Note:</strong> Field options do not appear until a type has been chosen and saved.'),
95 '#options' => $field_options,
98 $form = parent::buildConfigurationForm($form, $form_state);
106 public function getSummary() {
107 $summary = $this->t('Not yet configured.');
108 $paragraphs_type_id = $this->configuration['paragraphs_type'];
109 $paragraph_field_name = $this->configuration['paragraph_field'];
110 if ($paragraphs_type_id && $paragraphs_type = $this->paragraphsTypeStorage->load($paragraphs_type_id)) {
111 if ($paragraph_field_name && $paragraph_field = $this->fieldConfigStorage->load('paragraph.' . $paragraphs_type_id . '.' . $paragraph_field_name)) {
112 $summary = $this->t('Using the %field field on a %type paragraph.', [
113 '%field' => $paragraph_field->label(),
114 '%type' => $paragraphs_type->label(),
118 return $summary . '<br>' . parent::getSummary();
124 protected function prepareValue($delta, array &$values) {
125 parent::prepareValue($delta, $values);
126 $paragraph = $this->paragraphStorage->create([
127 'type' => $this->configuration['paragraphs_type'],
128 $this->configuration['paragraph_field'] => $values,
130 $values = ['entity' => $paragraph];