b7baaa55578122386ea8ff347e58c945a7e6e33c
[yaffs-website] / web / core / modules / media / tests / modules / media_test_source / src / Plugin / media / Source / Test.php
1 <?php
2
3 namespace Drupal\media_test_source\Plugin\media\Source;
4
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\media\MediaInterface;
8 use Drupal\media\MediaSourceBase;
9
10 /**
11  * Provides test media source.
12  *
13  * @MediaSource(
14  *   id = "test",
15  *   label = @Translation("Test source"),
16  *   description = @Translation("Test media source."),
17  *   allowed_field_types = {"string"},
18  * )
19  */
20 class Test extends MediaSourceBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public function getMetadataAttributes() {
26     // The metadata attributes are kept in state storage. This allows tests to
27     // change the metadata attributes and makes it easier to test different
28     // variations.
29     $attributes = \Drupal::state()->get('media_source_test_attributes', [
30       'attribute_1' => ['label' => $this->t('Attribute 1'), 'value' => 'Value 1'],
31       'attribute_2' => ['label' => $this->t('Attribute 2'), 'value' => 'Value 1'],
32     ]);
33     return array_map(function ($item) {
34       return $item['label'];
35     }, $attributes);
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function getMetadata(MediaInterface $media, $attribute_name) {
42     $attributes = \Drupal::state()->get('media_source_test_attributes', [
43       'attribute_1' => ['label' => $this->t('Attribute 1'), 'value' => 'Value 1'],
44       'attribute_2' => ['label' => $this->t('Attribute 2'), 'value' => 'Value 1'],
45     ]);
46
47     if (in_array($attribute_name, array_keys($attributes))) {
48       return $attributes[$attribute_name]['value'];
49     }
50
51     return parent::getMetadata($media, $attribute_name);
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getPluginDefinition() {
58     return NestedArray::mergeDeep(
59       parent::getPluginDefinition(),
60       \Drupal::state()->get('media_source_test_definition', [])
61     );
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function defaultConfiguration() {
68     return parent::defaultConfiguration() + [
69       'test_config_value' => 'This is default value.',
70     ];
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
77     $form = parent::buildConfigurationForm($form, $form_state);
78
79     $form['test_config_value'] = [
80       '#type' => 'textfield',
81       '#title' => $this->t('Test config value'),
82       '#default_value' => $this->configuration['test_config_value'],
83     ];
84
85     return $form;
86   }
87
88 }