PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/node/src/Plugin/views/wizard/Node.php

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