PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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