Version 1
[yaffs-website] / web / modules / contrib / paragraphs / src / Feeds / Target / Paragraphs.php
1 <?php
2
3 namespace Drupal\paragraphs\Feeds\Target;
4
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;
11
12 /**
13  * Feeds target plugin for Paragraphs fields.
14  *
15  * @FeedsTarget(
16  *   id = "paragraphs",
17  *   field_types = {"entity_reference_revisions"},
18  *   arguments = {"@entity.manager", "@current_user"}
19  * )
20  */
21 class Paragraphs extends Text implements ConfigurableTargetInterface {
22
23   /**
24    * The paragraph storage.
25    *
26    * @var \Drupal\Core\Entity\EntityStorageInterface
27    */
28   protected $paragraphStorage;
29
30   /**
31    * The paragraphs type storage.
32    *
33    * @var \Drupal\Core\Entity\EntityStorageInterface
34    */
35   protected $paragraphsTypeStorage;
36
37   /**
38    * The field config storage.
39    *
40    * @var \Drupal\Core\Entity\EntityStorageInterface
41    */
42   protected $fieldConfigStorage;
43
44   /**
45    * Constructs the target plugin.
46    */
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');
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function defaultConfiguration() {
58     return parent::defaultConfiguration() + [
59       'paragraphs_type' => NULL,
60       'paragraph_field' => NULL,
61     ];
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
68     $form['paragraphs_type'] = [
69       '#type' => 'select',
70       '#title' => $this->t('Paragraphs type'),
71       '#required' => TRUE,
72       '#options' => array_map(function(EntityInterface $paragraphs_type) {
73         return $paragraphs_type->label();
74       }, $this->paragraphsTypeStorage->loadMultiple()),
75       '#default_value' => $this->configuration['paragraphs_type'],
76     ];
77
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'],
83     ]);
84     $field_options = [];
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();
88       }
89     }
90
91     $form['paragraph_field'] = [
92       '#type' => 'select',
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,
96     ];
97
98     $form = parent::buildConfigurationForm($form, $form_state);
99
100     return $form;
101   }
102
103   /**
104    * {@inheritdoc}
105    */
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(),
115         ]);
116       }
117     }
118     return $summary . '<br>' . parent::getSummary();
119   }
120
121   /**
122    * {@inheritdoc}
123    */
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,
129     ]);
130     $values = ['entity' => $paragraph];
131   }
132
133 }