"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/node/src/Plugin/views/wizard/Node.php" (26 Nov 2020, 12683 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Node.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\node\Plugin\views\wizard;
    4 
    5 use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
    6 use Drupal\Core\Entity\EntityFieldManagerInterface;
    7 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
    8 use Drupal\Core\Field\FieldDefinitionInterface;
    9 use Drupal\Core\Form\FormStateInterface;
   10 use Drupal\views\Plugin\views\wizard\WizardPluginBase;
   11 use Symfony\Component\DependencyInjection\ContainerInterface;
   12 
   13 /**
   14  * @todo: replace numbers with constants.
   15  */
   16 
   17 /**
   18  * Tests creating node views with the wizard.
   19  *
   20  * @ViewsWizard(
   21  *   id = "node",
   22  *   base_table = "node_field_data",
   23  *   title = @Translation("Content")
   24  * )
   25  */
   26 class Node extends WizardPluginBase {
   27 
   28   /**
   29    * Set the created column.
   30    *
   31    * @var string
   32    */
   33   protected $createdColumn = 'node_field_data-created';
   34 
   35   /**
   36    * The entity display repository.
   37    *
   38    * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   39    */
   40   protected $entityDisplayRepository;
   41 
   42   /**
   43    * The entity field manager.
   44    *
   45    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   46    */
   47   protected $entityFieldManager;
   48 
   49   /**
   50    * Node constructor.
   51    *
   52    * @param array $configuration
   53    *   The plugin configuration.
   54    * @param string $plugin_id
   55    *   The plugin ID.
   56    * @param mixed $plugin_definition
   57    *   The plugin definition.
   58    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info_service
   59    *   The entity bundle info service.
   60    * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
   61    *   The entity display repository service.
   62    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   63    *   The entity field manager.
   64    */
   65   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeBundleInfoInterface $bundle_info_service, EntityDisplayRepositoryInterface $entity_display_repository = NULL, EntityFieldManagerInterface $entity_field_manager = NULL) {
   66     parent::__construct($configuration, $plugin_id, $plugin_definition, $bundle_info_service);
   67 
   68     if (!$entity_display_repository) {
   69       @trigger_error('The entity_display.repository service must be passed to ' . __METHOD__ . ', it is required before Drupal 9.0.0. See https://www.drupal.org/node/2835616.', E_USER_DEPRECATED);
   70       $entity_display_repository = \Drupal::service('entity_display.repository');
   71     }
   72     $this->entityDisplayRepository = $entity_display_repository;
   73     if (!$entity_field_manager) {
   74       @trigger_error('The entity_field.manager service must be passed to ' . __METHOD__ . ', it is required before Drupal 9.0.0. See https://www.drupal.org/node/2835616.', E_USER_DEPRECATED);
   75       $entity_field_manager = \Drupal::service('entity_field.manager');
   76     }
   77     $this->entityFieldManager = $entity_field_manager;
   78   }
   79 
   80   /**
   81    * {@inheritdoc}
   82    */
   83   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
   84     return new static(
   85       $configuration,
   86       $plugin_id,
   87       $plugin_definition,
   88       $container->get('entity_type.bundle.info'),
   89       $container->get('entity_display.repository'),
   90       $container->get('entity_field.manager')
   91     );
   92   }
   93 
   94   /**
   95    * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::getAvailableSorts().
   96    *
   97    * @return array
   98    *   An array whose keys are the available sort options and whose
   99    *   corresponding values are human readable labels.
  100    */
  101   public function getAvailableSorts() {
  102     // You can't execute functions in properties, so override the method
  103     return [
  104       'node_field_data-title:ASC' => $this->t('Title'),
  105     ];
  106   }
  107 
  108   /**
  109    * {@inheritdoc}
  110    */
  111   protected function rowStyleOptions() {
  112     $options = [];
  113     $options['teasers'] = $this->t('teasers');
  114     $options['full_posts'] = $this->t('full posts');
  115     $options['titles'] = $this->t('titles');
  116     $options['titles_linked'] = $this->t('titles (linked)');
  117     $options['fields'] = $this->t('fields');
  118     return $options;
  119   }
  120 
  121   /**
  122    * {@inheritdoc}
  123    */
  124   protected function defaultDisplayOptions() {
  125     $display_options = parent::defaultDisplayOptions();
  126 
  127     // Add permission-based access control.
  128     $display_options['access']['type'] = 'perm';
  129     $display_options['access']['options']['perm'] = 'access content';
  130 
  131     // Remove the default fields, since we are customizing them here.
  132     unset($display_options['fields']);
  133 
  134     // Add the title field, so that the display has content if the user switches
  135     // to a row style that uses fields.
  136     /* Field: Content: Title */
  137     $display_options['fields']['title']['id'] = 'title';
  138     $display_options['fields']['title']['table'] = 'node_field_data';
  139     $display_options['fields']['title']['field'] = 'title';
  140     $display_options['fields']['title']['entity_type'] = 'node';
  141     $display_options['fields']['title']['entity_field'] = 'title';
  142     $display_options['fields']['title']['label'] = '';
  143     $display_options['fields']['title']['alter']['alter_text'] = 0;
  144     $display_options['fields']['title']['alter']['make_link'] = 0;
  145     $display_options['fields']['title']['alter']['absolute'] = 0;
  146     $display_options['fields']['title']['alter']['trim'] = 0;
  147     $display_options['fields']['title']['alter']['word_boundary'] = 0;
  148     $display_options['fields']['title']['alter']['ellipsis'] = 0;
  149     $display_options['fields']['title']['alter']['strip_tags'] = 0;
  150     $display_options['fields']['title']['alter']['html'] = 0;
  151     $display_options['fields']['title']['hide_empty'] = 0;
  152     $display_options['fields']['title']['empty_zero'] = 0;
  153     $display_options['fields']['title']['settings']['link_to_entity'] = 1;
  154     $display_options['fields']['title']['plugin_id'] = 'field';
  155 
  156     return $display_options;
  157   }
  158 
  159   /**
  160    * {@inheritdoc}
  161    */
  162   protected function defaultDisplayFiltersUser(array $form, FormStateInterface $form_state) {
  163     $filters = parent::defaultDisplayFiltersUser($form, $form_state);
  164 
  165     $tids = [];
  166     if ($values = $form_state->getValue(['show', 'tagged_with'])) {
  167       foreach ($values as $value) {
  168         $tids[] = $value['target_id'];
  169       }
  170     }
  171     if (!empty($tids)) {
  172       $vid = reset($form['displays']['show']['tagged_with']['#selection_settings']['target_bundles']);
  173       $filters['tid'] = [
  174         'id' => 'tid',
  175         'table' => 'taxonomy_index',
  176         'field' => 'tid',
  177         'value' => $tids,
  178         'vid' => $vid,
  179         'plugin_id' => 'taxonomy_index_tid',
  180       ];
  181       // If the user entered more than one valid term in the autocomplete
  182       // field, they probably intended both of them to be applied.
  183       if (count($tids) > 1) {
  184         $filters['tid']['operator'] = 'and';
  185         // Sort the terms so the filter will be displayed as it normally would
  186         // on the edit screen.
  187         sort($filters['tid']['value']);
  188       }
  189     }
  190 
  191     return $filters;
  192   }
  193 
  194   /**
  195    * {@inheritdoc}
  196    */
  197   protected function pageDisplayOptions(array $form, FormStateInterface $form_state) {
  198     $display_options = parent::pageDisplayOptions($form, $form_state);
  199     $row_plugin = $form_state->getValue(['page', 'style', 'row_plugin']);
  200     $row_options = $form_state->getValue(['page', 'style', 'row_options'], []);
  201     $this->display_options_row($display_options, $row_plugin, $row_options);
  202     return $display_options;
  203   }
  204 
  205   /**
  206    * {@inheritdoc}
  207    */
  208   protected function blockDisplayOptions(array $form, FormStateInterface $form_state) {
  209     $display_options = parent::blockDisplayOptions($form, $form_state);
  210     $row_plugin = $form_state->getValue(['block', 'style', 'row_plugin']);
  211     $row_options = $form_state->getValue(['block', 'style', 'row_options'], []);
  212     $this->display_options_row($display_options, $row_plugin, $row_options);
  213     return $display_options;
  214   }
  215 
  216   /**
  217    * Set the row style and row style plugins to the display_options.
  218    */
  219   protected  function display_options_row(&$display_options, $row_plugin, $row_options) {
  220     switch ($row_plugin) {
  221       case 'full_posts':
  222         $display_options['row']['type'] = 'entity:node';
  223         $display_options['row']['options']['view_mode'] = 'full';
  224         break;
  225 
  226       case 'teasers':
  227         $display_options['row']['type'] = 'entity:node';
  228         $display_options['row']['options']['view_mode'] = 'teaser';
  229         break;
  230 
  231       case 'titles_linked':
  232       case 'titles':
  233         $display_options['row']['type'] = 'fields';
  234         $display_options['fields']['title']['id'] = 'title';
  235         $display_options['fields']['title']['table'] = 'node_field_data';
  236         $display_options['fields']['title']['field'] = 'title';
  237         $display_options['fields']['title']['settings']['link_to_entity'] = $row_plugin === 'titles_linked';
  238         $display_options['fields']['title']['plugin_id'] = 'field';
  239         break;
  240     }
  241   }
  242 
  243   /**
  244    * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::buildFilters().
  245    *
  246    * Add some options for filter by taxonomy terms.
  247    */
  248   protected function buildFilters(&$form, FormStateInterface $form_state) {
  249     parent::buildFilters($form, $form_state);
  250 
  251     if (isset($form['displays']['show']['type'])) {
  252       $selected_bundle = static::getSelected($form_state, ['show', 'type'], 'all', $form['displays']['show']['type']);
  253     }
  254 
  255     // Add the "tagged with" filter to the view.
  256 
  257     // We construct this filter using taxonomy_index.tid (which limits the
  258     // filtering to a specific vocabulary) rather than
  259     // taxonomy_term_field_data.name (which matches terms in any vocabulary).
  260     // This is because it is a more commonly-used filter that works better with
  261     // the autocomplete UI, and also to avoid confusion with other vocabularies
  262     // on the site that may have terms with the same name but are not used for
  263     // free tagging.
  264 
  265     // The downside is that if there *is* more than one vocabulary on the site
  266     // that is used for free tagging, the wizard will only be able to make the
  267     // "tagged with" filter apply to one of them (see below for the method it
  268     // uses to choose).
  269 
  270     // Find all "tag-like" taxonomy fields associated with the view's
  271     // entities. If a particular entity type (i.e., bundle) has been
  272     // selected above, then we only search for taxonomy fields associated
  273     // with that bundle. Otherwise, we use all bundles.
  274     $bundles = array_keys($this->bundleInfoService->getBundleInfo($this->entityTypeId));
  275     // Double check that this is a real bundle before using it (since above
  276     // we added a dummy option 'all' to the bundle list on the form).
  277     if (isset($selected_bundle) && in_array($selected_bundle, $bundles)) {
  278       $bundles = [$selected_bundle];
  279     }
  280     $tag_fields = [];
  281     foreach ($bundles as $bundle) {
  282       $display = $this->entityDisplayRepository->getFormDisplay($this->entityTypeId, $bundle);
  283       $taxonomy_fields = array_filter($this->entityFieldManager->getFieldDefinitions($this->entityTypeId, $bundle), function (FieldDefinitionInterface $field_definition) {
  284         return $field_definition->getType() == 'entity_reference' && $field_definition->getSetting('target_type') == 'taxonomy_term';
  285       });
  286       foreach ($taxonomy_fields as $field_name => $field) {
  287         $widget = $display->getComponent($field_name);
  288         // We define "tag-like" taxonomy fields as ones that use the
  289         // "Autocomplete (Tags style)" widget.
  290         if ($widget['type'] == 'entity_reference_autocomplete_tags') {
  291           $tag_fields[$field_name] = $field;
  292         }
  293       }
  294     }
  295     if (!empty($tag_fields)) {
  296       // If there is more than one "tag-like" taxonomy field available to
  297       // the view, we can only make our filter apply to one of them (as
  298       // described above). We choose 'field_tags' if it is available, since
  299       // that is created by the Standard install profile in core and also
  300       // commonly used by contrib modules; thus, it is most likely to be
  301       // associated with the "main" free-tagging vocabulary on the site.
  302       if (array_key_exists('field_tags', $tag_fields)) {
  303         $tag_field_name = 'field_tags';
  304       }
  305       else {
  306         $tag_field_name = key($tag_fields);
  307       }
  308       // Add the autocomplete textfield to the wizard.
  309       $target_bundles = $tag_fields[$tag_field_name]->getSetting('handler_settings')['target_bundles'];
  310       $form['displays']['show']['tagged_with'] = [
  311         '#type' => 'entity_autocomplete',
  312         '#title' => $this->t('tagged with'),
  313         '#target_type' => 'taxonomy_term',
  314         '#selection_settings' => ['target_bundles' => $target_bundles],
  315         '#tags' => TRUE,
  316         '#size' => 30,
  317         '#maxlength' => 1024,
  318       ];
  319     }
  320   }
  321 
  322 }