3 namespace Drupal\media_test_source\Plugin\media\Source;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\media\MediaInterface;
8 use Drupal\media\MediaSourceBase;
11 * Provides test media source.
15 * label = @Translation("Test source"),
16 * description = @Translation("Test media source."),
17 * allowed_field_types = {"string"},
20 class Test extends MediaSourceBase {
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
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'],
33 return array_map(function ($item) {
34 return $item['label'];
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'],
47 if (in_array($attribute_name, array_keys($attributes))) {
48 return $attributes[$attribute_name]['value'];
51 return parent::getMetadata($media, $attribute_name);
57 public function getPluginDefinition() {
58 return NestedArray::mergeDeep(
59 parent::getPluginDefinition(),
60 \Drupal::state()->get('media_source_test_definition', [])
67 public function defaultConfiguration() {
68 return parent::defaultConfiguration() + [
69 'test_config_value' => 'This is default value.',
76 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
77 $form = parent::buildConfigurationForm($form, $form_state);
79 $form['test_config_value'] = [
80 '#type' => 'textfield',
81 '#title' => $this->t('Test config value'),
82 '#default_value' => $this->configuration['test_config_value'],