Version 1
[yaffs-website] / web / core / modules / views / src / Plugin / views / row / RssPluginBase.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\row;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8
9 /**
10  * Base class for Views RSS row plugins.
11  */
12 abstract class RssPluginBase extends RowPluginBase {
13
14   /**
15    * The entity manager.
16    *
17    * @var \Drupal\Core\Entity\EntityManagerInterface
18    */
19   protected $entityManager;
20
21   /**
22    * Constructs a RssPluginBase  object.
23    *
24    * @param array $configuration
25    *   A configuration array containing information about the plugin instance.
26    * @param string $plugin_id
27    *   The plugin_id for the plugin instance.
28    * @param mixed $plugin_definition
29    *   The plugin implementation definition.
30    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
31    *   The entity manager.
32    */
33   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
34     parent::__construct($configuration, $plugin_id, $plugin_definition);
35
36     $this->entityManager = $entity_manager;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
43     return new static(
44       $configuration,
45       $plugin_id,
46       $plugin_definition,
47       $container->get('entity.manager')
48     );
49   }
50
51   /**
52    * The ID of the entity type for which this is an RSS row plugin.
53    *
54    * @var string
55    */
56   protected $entityTypeId;
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function defineOptions() {
62     $options = parent::defineOptions();
63
64     $options['view_mode'] = ['default' => 'default'];
65
66     return $options;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
73     parent::buildOptionsForm($form, $form_state);
74
75     $form['view_mode'] = [
76       '#type' => 'select',
77       '#title' => $this->t('Display type'),
78       '#options' => $this->buildOptionsForm_summary_options(),
79       '#default_value' => $this->options['view_mode'],
80     ];
81   }
82
83   /**
84    * Return the main options, which are shown in the summary title.
85    */
86   public function buildOptionsForm_summary_options() {
87     $view_modes = $this->entityManager->getViewModes($this->entityTypeId);
88     $options = [];
89     foreach ($view_modes as $mode => $settings) {
90       $options[$mode] = $settings['label'];
91     }
92     return $options;
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function calculateDependencies() {
99     $dependencies = parent::calculateDependencies();
100
101     $view_mode = $this->entityManager
102       ->getStorage('entity_view_mode')
103       ->load($this->entityTypeId . '.' . $this->options['view_mode']);
104     if ($view_mode) {
105       $dependencies[$view_mode->getConfigDependencyKey()][] = $view_mode->getConfigDependencyName();
106     }
107
108     return $dependencies;
109   }
110
111 }