RssPluginBase.php in Drupal 8        
                          
                  
                        
  
  
  
  
File
  core/modules/views/src/Plugin/views/row/RssPluginBase.php
  
    View source  
  <?php
namespace Drupal\views\Plugin\views\row;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class RssPluginBase extends RowPluginBase {
  use DeprecatedServicePropertyTrait;
  
  protected $deprecatedProperties = [
    'entityManager' => 'entity.manager',
  ];
  
  protected $entityTypeManager;
  
  protected $entityDisplayRepository;
  
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    if (!$entity_display_repository) {
      @trigger_error('Calling RssPluginBase::__construct() with the $entity_repository argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
      $entity_display_repository = \Drupal::service('entity_display.repository');
    }
    $this->entityDisplayRepository = $entity_display_repository;
  }
  
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('entity_display.repository'));
  }
  
  protected $entityTypeId;
  
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['view_mode'] = [
      'default' => 'default',
    ];
    return $options;
  }
  
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['view_mode'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Display type'),
      '#options' => $this
        ->buildOptionsForm_summary_options(),
      '#default_value' => $this->options['view_mode'],
    ];
  }
  
  public function buildOptionsForm_summary_options() {
    $view_modes = $this->entityDisplayRepository
      ->getViewModes($this->entityTypeId);
    $options = [];
    foreach ($view_modes as $mode => $settings) {
      $options[$mode] = $settings['label'];
    }
    return $options;
  }
  
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();
    $view_mode = $this->entityTypeManager
      ->getStorage('entity_view_mode')
      ->load($this->entityTypeId . '.' . $this->options['view_mode']);
    if ($view_mode) {
      $dependencies[$view_mode
        ->getConfigDependencyKey()][] = $view_mode
        ->getConfigDependencyName();
    }
    return $dependencies;
  }
}