e2389f5301d415c5f91a6f37bec236c205addf4d
[yaffs-website] / web / core / modules / media / tests / modules / media_test_source / src / Plugin / media / Source / TestDifferentDisplays.php
1 <?php
2
3 namespace Drupal\media_test_source\Plugin\media\Source;
4
5 use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
6 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
7 use Drupal\media\MediaTypeInterface;
8
9 /**
10  * Provides test media source.
11  *
12  * @MediaSource(
13  *   id = "test_different_displays",
14  *   label = @Translation("Test source with different displays"),
15  *   description = @Translation("Test source with different displays."),
16  *   allowed_field_types = {"entity_reference"},
17  * )
18  */
19 class TestDifferentDisplays extends Test {
20
21   /**
22    * {@inheritdoc}
23    */
24   public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
25     parent::prepareViewDisplay($type, $display);
26     $source_name = $this->getSourceFieldDefinition($type)->getName();
27     $source_component = $display->getComponent($source_name) ?: [];
28     $source_component['type'] = 'entity_reference_entity_id';
29     $display->setComponent($source_name, $source_component);
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) {
36     parent::prepareFormDisplay($type, $display);
37     $source_name = $this->getSourceFieldDefinition($type)->getName();
38     $source_component = $display->getComponent($source_name) ?: [];
39     $source_component['type'] = 'entity_reference_autocomplete_tags';
40     $display->setComponent($source_name, $source_component);
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   protected function getSourceFieldName() {
47     return 'field_media_different_display';
48   }
49
50 }