3 namespace Drupal\Tests\paragraphs\FunctionalJavascript;
5 use Drupal\Core\Entity\Entity\EntityFormDisplay;
6 use Drupal\Core\Entity\Entity\EntityViewDisplay;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\file\Entity\File;
10 use Drupal\node\Entity\NodeType;
11 use Drupal\paragraphs\Entity\ParagraphsType;
12 use Drupal\Tests\TestFileCreationTrait;
15 * Test trait for Paragraphs JS tests.
17 trait ParagraphsTestBaseTrait {
19 use TestFileCreationTrait;
22 * Adds a content type with a Paragraphs field.
24 * @param string $content_type_name
25 * Content type name to be used.
26 * @param string $paragraphs_field_name
27 * (optional) Field name to be used. Defaults to 'field_paragraphs'.
28 * @param string $widget_type
29 * (optional) Declares if we use experimental or classic widget.
30 * Defaults to 'paragraphs' for experimental widget.
31 * Use 'entity_reference_paragraphs' for classic widget.
33 protected function addParagraphedContentType($content_type_name, $paragraphs_field_name = 'field_paragraphs', $widget_type = 'paragraphs') {
34 // Create the content type.
35 $node_type = NodeType::create([
36 'type' => $content_type_name,
37 'name' => $content_type_name,
41 $this->addParagraphsField($content_type_name, $paragraphs_field_name, 'node', $widget_type);
45 * Adds a Paragraphs field to a given entity type.
47 * @param string $entity_type_name
48 * Entity type name to be used.
49 * @param string $paragraphs_field_name
50 * Paragraphs field name to be used.
51 * @param string $entity_type
52 * Entity type where to add the field.
53 * @param string $widget_type
54 * (optional) Declares if we use experimental or classic widget.
55 * Defaults to 'paragraphs' for experimental widget.
56 * Use 'entity_reference_paragraphs' for classic widget.
58 protected function addParagraphsField($entity_type_name, $paragraphs_field_name, $entity_type, $widget_type = 'paragraphs') {
59 $field_storage = FieldStorageConfig::loadByName($entity_type, $paragraphs_field_name);
60 if (!$field_storage) {
61 // Add a paragraphs field.
62 $field_storage = FieldStorageConfig::create([
63 'field_name' => $paragraphs_field_name,
64 'entity_type' => $entity_type,
65 'type' => 'entity_reference_revisions',
66 'cardinality' => '-1',
68 'target_type' => 'paragraph',
71 $field_storage->save();
73 $field = FieldConfig::create([
74 'field_storage' => $field_storage,
75 'bundle' => $entity_type_name,
77 'handler' => 'default:paragraph',
78 'handler_settings' => ['target_bundles' => NULL],
83 $form_display = entity_get_form_display($entity_type, $entity_type_name, 'default')
84 ->setComponent($paragraphs_field_name, ['type' => $widget_type]);
85 $form_display->save();
87 $view_display = entity_get_display($entity_type, $entity_type_name, 'default')
88 ->setComponent($paragraphs_field_name, ['type' => 'entity_reference_revisions_entity_view']);
89 $view_display->save();
93 * Adds a Paragraphs type.
95 * @param string $paragraphs_type_name
96 * Paragraph type name used to create.
98 protected function addParagraphsType($paragraphs_type_name) {
99 $paragraphs_type = ParagraphsType::create([
100 'id' => $paragraphs_type_name,
101 'label' => $paragraphs_type_name,
103 $paragraphs_type->save();
107 * Adds an icon to a paragraphs type.
109 * @param string $paragraphs_type
110 * Machine name of the paragraph type to add the icon to.
112 * @return \Drupal\file\Entity\File
113 * The file entity used as the icon.
115 protected function addParagraphsTypeIcon($paragraphs_type) {
117 $image_files = $this->getTestFiles('image');
118 $uri = current($image_files)->uri;
120 // Create a copy of the image, so that multiple file entities don't
121 // reference the same file.
122 $copy_uri = file_unmanaged_copy($uri);
124 // Create a new file entity.
125 $file_entity = File::create([
128 $file_entity->save();
130 // Add the file entity to the paragraphs type as its icon.
131 $paragraphs_type_entity = ParagraphsType::load($paragraphs_type);
132 $paragraphs_type_entity->set('icon_uuid', $file_entity->uuid());
133 $paragraphs_type_entity->save();
139 * Adds a field to a given paragraph type.
141 * @param string $paragraph_type_id
142 * Paragraph type ID to be used.
143 * @param string $field_name
144 * Field name to be used.
145 * @param string $field_type
147 * @param array $storage_settings
148 * Settings for the field storage.
150 protected function addFieldtoParagraphType($paragraph_type_id, $field_name, $field_type, array $storage_settings = []) {
151 // Add a paragraphs field.
152 $field_storage = FieldStorageConfig::create([
153 'field_name' => $field_name,
154 'entity_type' => 'paragraph',
155 'type' => $field_type,
157 'settings' => $storage_settings,
159 $field_storage->save();
160 $field = FieldConfig::create([
161 'field_storage' => $field_storage,
162 'bundle' => $paragraph_type_id,
167 $field_type_definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_type);
169 entity_get_form_display('paragraph', $paragraph_type_id, 'default')
170 ->setComponent($field_name, ['type' => $field_type_definition['default_widget']])
173 entity_get_display('paragraph', $paragraph_type_id, 'default')
174 ->setComponent($field_name, ['type' => $field_type_definition['default_formatter']])
179 * Sets some of the settings of a paragraphs field widget.
181 * @param string $bundle
182 * Machine name of the bundle (e.g., a content type for nodes, a paragraphs
183 * type for paragraphs, etc.) with a paragraphs field.
184 * @param string $paragraphs_field
185 * Machine name of the paragraphs field whose widget (settings) to change.
186 * @param array $settings
187 * New setting values keyed by names of settings that are to be set.
188 * @param string|null $field_widget
189 * (optional) Machine name of the paragraphs field widget to use. NULL to
190 * keep the current widget.
191 * @param string $entity_type
192 * (optional) Machine name of the content entity type that the bundle
193 * belongs to. Defaults to "node".
195 protected function setParagraphsWidgetSettings($bundle, $paragraphs_field, array $settings, $field_widget = NULL, $entity_type = 'node') {
196 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $default_form_display */
197 $default_form_display = \Drupal::entityTypeManager()
198 ->getStorage('entity_form_display')
199 ->load($entity_type . '.' . $bundle . '.default');
200 $component = $default_form_display->getComponent($paragraphs_field);
202 $updated_component = $component;
203 if ($field_widget === NULL || $field_widget === $component['type']) {
204 // The widget stays the same.
205 $updated_component['settings'] = $settings + $component['settings'];
208 // Change the widget.
209 $updated_component['type'] = $field_widget;
211 $widget_definition = \Drupal::service('plugin.manager.field.widget')
212 ->getDefinition($field_widget);
213 /** @var \Drupal\Core\Field\WidgetInterface $class */
214 $class = $widget_definition['class'];
215 $default_settings = $class::defaultSettings();
217 $updated_component['settings'] = $settings + $default_settings;
220 $default_form_display->setComponent($paragraphs_field, $updated_component)