Interim commit.
[yaffs-website] / web / modules / contrib / video_embed_field / tests / src / Functional / EntityDisplaySetupTrait.php
1 <?php
2
3 namespace Drupal\Tests\video_embed_field\Functional;
4 use Drupal\field\Entity\FieldConfig;
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\simpletest\ContentTypeCreationTrait;
7 use Drupal\simpletest\NodeCreationTrait;
8
9 /**
10  * A trait for manipulating entity display.
11  */
12 trait EntityDisplaySetupTrait {
13
14   use ContentTypeCreationTrait;
15   use NodeCreationTrait;
16
17   /**
18    * The field name.
19    *
20    * @var string
21    */
22   protected $fieldName;
23
24   /**
25    * The name of the content type.
26    *
27    * @var string
28    */
29   protected $contentTypeName;
30
31   /**
32    * The entity display.
33    *
34    * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
35    */
36   protected $entityDisplay;
37
38   /**
39    * The form display.
40    *
41    * @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface
42    */
43   protected $entityFormDisplay;
44
45   /**
46    * Setup the entity displays with required fields.
47    */
48   protected function setupEntityDisplays() {
49     $this->fieldName = 'field_test_video_field';
50     $this->contentTypeName = 'test_content_type_name';
51     $this->createContentType(['type' => $this->contentTypeName]);
52     $field_storage = FieldStorageConfig::create([
53       'field_name' => $this->fieldName,
54       'entity_type' => 'node',
55       'type' => 'video_embed_field',
56       'settings' => [
57         'allowed_providers' => [],
58       ],
59     ]);
60     $field_storage->save();
61     FieldConfig::create([
62       'field_storage' => $field_storage,
63       'bundle' => $this->contentTypeName,
64       'settings' => [],
65     ])->save();
66     $this->entityDisplay = entity_get_display('node', $this->contentTypeName, 'default');
67     $this->entityFormDisplay = entity_get_form_display('node', $this->contentTypeName, 'default');
68   }
69
70   /**
71    * Set component settings for the display.
72    *
73    * @param string $type
74    *   The component to change settings for.
75    * @param array $settings
76    *   The settings to use.
77    */
78   protected function setDisplayComponentSettings($type, $settings = []) {
79     $this->entityDisplay->setComponent($this->fieldName, [
80       'type' => $type,
81       'settings' => $settings,
82     ])->save();
83   }
84
85   /**
86    * Set component settings for the form.
87    *
88    * @param string $type
89    *   The component to change settings for.
90    * @param array $settings
91    *   The settings to use.
92    */
93   protected function setFormComponentSettings($type, $settings = []) {
94     $this->entityFormDisplay
95       ->setComponent($this->fieldName, [
96         'type' => $type,
97         'settings' => $settings,
98       ])
99       ->save();
100   }
101
102   /**
103    * Create a video node using the video field.
104    *
105    * @param string $value
106    *   The video URL to use for the field value.
107    *
108    * @return \Drupal\node\NodeInterface
109    *   A node.
110    */
111   protected function createVideoNode($value) {
112     return $this->createNode([
113       'type' => $this->contentTypeName,
114       $this->fieldName => [
115         ['value' => $value],
116       ],
117     ]);
118   }
119
120 }