PageRenderTime 37ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/core/modules/views/src/Plugin/views/display/Block.php

https://gitlab.com/guillaumev/alkarama
PHP | 369 lines | 202 code | 38 blank | 129 comment | 15 complexity | 82f039363bac247d714f91b4d745e997 MD5 | raw file
  1. <?php
  2. namespace Drupal\views\Plugin\views\display;
  3. use Drupal\Core\Entity\EntityManagerInterface;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\views\Plugin\Block\ViewsBlock;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. /**
  8. * The plugin that handles a block.
  9. *
  10. * @ingroup views_display_plugins
  11. *
  12. * @ViewsDisplay(
  13. * id = "block",
  14. * title = @Translation("Block"),
  15. * help = @Translation("Display the view as a block."),
  16. * theme = "views_view",
  17. * register_theme = FALSE,
  18. * uses_hook_block = TRUE,
  19. * contextual_links_locations = {"block"},
  20. * admin = @Translation("Block")
  21. * )
  22. *
  23. * @see \Drupal\views\Plugin\Block\ViewsBlock
  24. * @see \Drupal\views\Plugin\Derivative\ViewsBlock
  25. */
  26. class Block extends DisplayPluginBase {
  27. /**
  28. * Whether the display allows attachments.
  29. *
  30. * @var bool
  31. */
  32. protected $usesAttachments = TRUE;
  33. /**
  34. * The entity manager.
  35. *
  36. * @var \Drupal\Core\Entity\EntityManagerInterface
  37. */
  38. protected $entityManager;
  39. /**
  40. * Constructs a new Block instance.
  41. *
  42. * @param array $configuration
  43. * A configuration array containing information about the plugin instance.
  44. * @param string $plugin_id
  45. * The plugin_id for the plugin instance.
  46. * @param mixed $plugin_definition
  47. * The plugin implementation definition.
  48. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  49. * The entity manager.
  50. */
  51. public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
  52. parent::__construct($configuration, $plugin_id, $plugin_definition);
  53. $this->entityManager = $entity_manager;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  59. return new static(
  60. $configuration,
  61. $plugin_id,
  62. $plugin_definition,
  63. $container->get('entity.manager')
  64. );
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function defineOptions() {
  70. $options = parent::defineOptions();
  71. $options['block_description'] = array('default' => '');
  72. $options['block_category'] = array('default' => $this->t('Lists (Views)'));
  73. $options['block_hide_empty'] = array('default' => FALSE);
  74. $options['allow'] = array(
  75. 'contains' => array(
  76. 'items_per_page' => array('default' => 'items_per_page'),
  77. ),
  78. );
  79. return $options;
  80. }
  81. /**
  82. * Returns plugin-specific settings for the block.
  83. *
  84. * @param array $settings
  85. * The settings of the block.
  86. *
  87. * @return array
  88. * An array of block-specific settings to override the defaults provided in
  89. * \Drupal\views\Plugin\Block\ViewsBlock::defaultConfiguration().
  90. *
  91. * @see \Drupal\views\Plugin\Block\ViewsBlock::defaultConfiguration()
  92. */
  93. public function blockSettings(array $settings) {
  94. $settings['items_per_page'] = 'none';
  95. return $settings;
  96. }
  97. /**
  98. * The display block handler returns the structure necessary for a block.
  99. */
  100. public function execute() {
  101. // Prior to this being called, the $view should already be set to this
  102. // display, and arguments should be set on the view.
  103. $element = $this->view->render();
  104. if ($this->outputIsEmpty() && $this->getOption('block_hide_empty') && empty($this->view->style_plugin->definition['even empty'])) {
  105. return array();
  106. }
  107. else {
  108. return $element;
  109. }
  110. }
  111. /**
  112. * Provide the summary for page options in the views UI.
  113. *
  114. * This output is returned as an array.
  115. */
  116. public function optionsSummary(&$categories, &$options) {
  117. parent::optionsSummary($categories, $options);
  118. $categories['block'] = array(
  119. 'title' => $this->t('Block settings'),
  120. 'column' => 'second',
  121. 'build' => array(
  122. '#weight' => -10,
  123. ),
  124. );
  125. $block_description = strip_tags($this->getOption('block_description'));
  126. if (empty($block_description)) {
  127. $block_description = $this->t('None');
  128. }
  129. $block_category = $this->getOption('block_category');
  130. $options['block_description'] = array(
  131. 'category' => 'block',
  132. 'title' => $this->t('Block name'),
  133. 'value' => views_ui_truncate($block_description, 24),
  134. );
  135. $options['block_category'] = array(
  136. 'category' => 'block',
  137. 'title' => $this->t('Block category'),
  138. 'value' => views_ui_truncate($block_category, 24),
  139. );
  140. $filtered_allow = array_filter($this->getOption('allow'));
  141. $options['allow'] = array(
  142. 'category' => 'block',
  143. 'title' => $this->t('Allow settings'),
  144. 'value' => empty($filtered_allow) ? $this->t('None') : $this->t('Items per page'),
  145. );
  146. $options['block_hide_empty'] = array(
  147. 'category' => 'other',
  148. 'title' => $this->t('Hide block if the view output is empty'),
  149. 'value' => $this->getOption('block_hide_empty') ? $this->t('Yes') : $this->t('No'),
  150. );
  151. }
  152. /**
  153. * Provide the default form for setting options.
  154. */
  155. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  156. parent::buildOptionsForm($form, $form_state);
  157. switch ($form_state->get('section')) {
  158. case 'block_description':
  159. $form['#title'] .= $this->t('Block admin description');
  160. $form['block_description'] = array(
  161. '#type' => 'textfield',
  162. '#description' => $this->t('This will appear as the name of this block in administer >> structure >> blocks.'),
  163. '#default_value' => $this->getOption('block_description'),
  164. );
  165. break;
  166. case 'block_category':
  167. $form['#title'] .= $this->t('Block category');
  168. $form['block_category'] = array(
  169. '#type' => 'textfield',
  170. '#autocomplete_route_name' => 'block.category_autocomplete',
  171. '#description' => $this->t('The category this block will appear under on the <a href=":href">blocks placement page</a>.', array(':href' => \Drupal::url('block.admin_display'))),
  172. '#default_value' => $this->getOption('block_category'),
  173. );
  174. break;
  175. case 'block_hide_empty':
  176. $form['#title'] .= $this->t('Block empty settings');
  177. $form['block_hide_empty'] = array(
  178. '#title' => $this->t('Hide block if no result/empty text'),
  179. '#type' => 'checkbox',
  180. '#description' => $this->t('Hide the block if there is no result and no empty text and no header/footer which is shown on empty result'),
  181. '#default_value' => $this->getOption('block_hide_empty'),
  182. );
  183. break;
  184. case 'exposed_form_options':
  185. $this->view->initHandlers();
  186. if (!$this->usesExposed() && parent::usesExposed()) {
  187. $form['exposed_form_options']['warning'] = array(
  188. '#weight' => -10,
  189. '#markup' => '<div class="messages messages--warning">' . $this->t('Exposed filters in block displays require "Use AJAX" to be set to work correctly.') . '</div>',
  190. );
  191. }
  192. break;
  193. case 'allow':
  194. $form['#title'] .= $this->t('Allow settings in the block configuration');
  195. $options = array(
  196. 'items_per_page' => $this->t('Items per page'),
  197. );
  198. $allow = array_filter($this->getOption('allow'));
  199. $form['allow'] = array(
  200. '#type' => 'checkboxes',
  201. '#default_value' => $allow,
  202. '#options' => $options,
  203. );
  204. break;
  205. }
  206. }
  207. /**
  208. * Perform any necessary changes to the form values prior to storage.
  209. * There is no need for this function to actually store the data.
  210. */
  211. public function submitOptionsForm(&$form, FormStateInterface $form_state) {
  212. parent::submitOptionsForm($form, $form_state);
  213. $section = $form_state->get('section');
  214. switch ($section) {
  215. case 'block_description':
  216. case 'block_category':
  217. case 'allow':
  218. case 'block_hide_empty':
  219. $this->setOption($section, $form_state->getValue($section));
  220. break;
  221. }
  222. }
  223. /**
  224. * Adds the configuration form elements specific to this views block plugin.
  225. *
  226. * This method allows block instances to override the views items_per_page.
  227. *
  228. * @param \Drupal\views\Plugin\Block\ViewsBlock $block
  229. * The ViewsBlock plugin.
  230. * @param array $form
  231. * The form definition array for the block configuration form.
  232. * @param \Drupal\Core\Form\FormStateInterface $form_state
  233. * The current state of the form.
  234. *
  235. * @return array $form
  236. * The renderable form array representing the entire configuration form.
  237. *
  238. * @see \Drupal\views\Plugin\Block\ViewsBlock::blockForm()
  239. */
  240. public function blockForm(ViewsBlock $block, array &$form, FormStateInterface $form_state) {
  241. $allow_settings = array_filter($this->getOption('allow'));
  242. $block_configuration = $block->getConfiguration();
  243. foreach ($allow_settings as $type => $enabled) {
  244. if (empty($enabled)) {
  245. continue;
  246. }
  247. switch ($type) {
  248. case 'items_per_page':
  249. $form['override']['items_per_page'] = array(
  250. '#type' => 'select',
  251. '#title' => $this->t('Items per block'),
  252. '#options' => array(
  253. 'none' => $this->t('@count (default setting)', array('@count' => $this->getPlugin('pager')->getItemsPerPage())),
  254. 5 => 5,
  255. 10 => 10,
  256. 20 => 20,
  257. 40 => 40,
  258. ),
  259. '#default_value' => $block_configuration['items_per_page'],
  260. );
  261. break;
  262. }
  263. }
  264. return $form;
  265. }
  266. /**
  267. * Handles form validation for the views block configuration form.
  268. *
  269. * @param \Drupal\views\Plugin\Block\ViewsBlock $block
  270. * The ViewsBlock plugin.
  271. * @param array $form
  272. * The form definition array for the block configuration form.
  273. * @param \Drupal\Core\Form\FormStateInterface $form_state
  274. * The current state of the form.
  275. *
  276. * @see \Drupal\views\Plugin\Block\ViewsBlock::blockValidate()
  277. */
  278. public function blockValidate(ViewsBlock $block, array $form, FormStateInterface $form_state) {
  279. }
  280. /**
  281. * Handles form submission for the views block configuration form.
  282. *
  283. * @param \Drupal\views\Plugin\Block\ViewsBlock $block
  284. * The ViewsBlock plugin.
  285. * @param array $form
  286. * The form definition array for the full block configuration form.
  287. * @param \Drupal\Core\Form\FormStateInterface $form_state
  288. * The current state of the form.
  289. *
  290. * @see \Drupal\views\Plugin\Block\ViewsBlock::blockSubmit()
  291. */
  292. public function blockSubmit(ViewsBlock $block, $form, FormStateInterface $form_state) {
  293. if ($items_per_page = $form_state->getValue(array('override', 'items_per_page'))) {
  294. $block->setConfigurationValue('items_per_page', $items_per_page);
  295. }
  296. $form_state->unsetValue(array('override', 'items_per_page'));
  297. }
  298. /**
  299. * Allows to change the display settings right before executing the block.
  300. *
  301. * @param \Drupal\views\Plugin\Block\ViewsBlock $block
  302. * The block plugin for views displays.
  303. */
  304. public function preBlockBuild(ViewsBlock $block) {
  305. $config = $block->getConfiguration();
  306. if ($config['items_per_page'] !== 'none') {
  307. $this->view->setItemsPerPage($config['items_per_page']);
  308. }
  309. }
  310. /**
  311. * Block views use exposed widgets only if AJAX is set.
  312. */
  313. public function usesExposed() {
  314. if ($this->ajaxEnabled()) {
  315. return parent::usesExposed();
  316. }
  317. return FALSE;
  318. }
  319. /**
  320. * {@inheritdoc}
  321. */
  322. public function remove() {
  323. parent::remove();
  324. if ($this->entityManager->hasDefinition('block')) {
  325. $plugin_id = 'views_block:' . $this->view->storage->id() . '-' . $this->display['id'];
  326. foreach ($this->entityManager->getStorage('block')->loadByProperties(['plugin' => $plugin_id]) as $block) {
  327. $block->delete();
  328. }
  329. }
  330. }
  331. }