Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / paragraphs / tests / src / FunctionalJavascript / ParagraphsTestBaseTrait.php
1 <?php
2
3 namespace Drupal\Tests\paragraphs\FunctionalJavascript;
4
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;
13
14 /**
15  * Test trait for Paragraphs JS tests.
16  */
17 trait ParagraphsTestBaseTrait {
18
19   use TestFileCreationTrait;
20
21   /**
22    * Adds a content type with a Paragraphs field.
23    *
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.
32    */
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,
38     ]);
39     $node_type->save();
40
41     $this->addParagraphsField($content_type_name, $paragraphs_field_name, 'node', $widget_type);
42   }
43
44   /**
45    * Adds a Paragraphs field to a given entity type.
46    *
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.
57    */
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',
67         'settings' => [
68           'target_type' => 'paragraph',
69         ],
70       ]);
71       $field_storage->save();
72     }
73     $field = FieldConfig::create([
74       'field_storage' => $field_storage,
75       'bundle' => $entity_type_name,
76       'settings' => [
77         'handler' => 'default:paragraph',
78         'handler_settings' => ['target_bundles' => NULL],
79       ],
80     ]);
81     $field->save();
82
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();
86
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();
90   }
91
92   /**
93    * Adds a Paragraphs type.
94    *
95    * @param string $paragraphs_type_name
96    *   Paragraph type name used to create.
97    */
98   protected function addParagraphsType($paragraphs_type_name) {
99     $paragraphs_type = ParagraphsType::create([
100       'id' => $paragraphs_type_name,
101       'label' => $paragraphs_type_name,
102     ]);
103     $paragraphs_type->save();
104   }
105
106   /**
107    * Adds an icon to a paragraphs type.
108    *
109    * @param string $paragraphs_type
110    *   Machine name of the paragraph type to add the icon to.
111    *
112    * @return \Drupal\file\Entity\File
113    *   The file entity used as the icon.
114    */
115   protected function addParagraphsTypeIcon($paragraphs_type) {
116     // Get an image.
117     $image_files = $this->getTestFiles('image');
118     $uri = current($image_files)->uri;
119
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);
123
124     // Create a new file entity.
125     $file_entity = File::create([
126       'uri' => $copy_uri,
127     ]);
128     $file_entity->save();
129
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();
134
135     return $file_entity;
136   }
137
138   /**
139    * Adds a field to a given paragraph type.
140    *
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
146    *   Type of the field.
147    * @param array $storage_settings
148    *   Settings for the field storage.
149    */
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,
156       'cardinality' => 1,
157       'settings' => $storage_settings,
158     ]);
159     $field_storage->save();
160     $field = FieldConfig::create([
161       'field_storage' => $field_storage,
162       'bundle' => $paragraph_type_id,
163       'settings' => [],
164     ]);
165     $field->save();
166
167     $field_type_definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_type);
168
169     entity_get_form_display('paragraph', $paragraph_type_id, 'default')
170       ->setComponent($field_name, ['type' => $field_type_definition['default_widget']])
171       ->save();
172
173     entity_get_display('paragraph', $paragraph_type_id, 'default')
174       ->setComponent($field_name, ['type' => $field_type_definition['default_formatter']])
175       ->save();
176   }
177
178   /**
179    * Sets some of the settings of a paragraphs field widget.
180    *
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".
194    */
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);
201
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'];
206     }
207     else {
208       // Change the widget.
209       $updated_component['type'] = $field_widget;
210
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();
216
217       $updated_component['settings'] = $settings + $default_settings;
218     }
219
220     $default_form_display->setComponent($paragraphs_field, $updated_component)
221       ->save();
222   }
223
224 }