Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. <?php
  2. use Drupal\Core\Cache\CacheBackendInterface;
  3. use Drupal\Core\StringTranslation\TranslatableMarkup;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Entity\EntityInterface;
  6.  
  7. /**
  8. * Implements hook_form_FORM_ID_alter().
  9. */
  10. function myModule_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  11.  
  12. if (isset($form['#id']) && $form['#id'] == 'views-exposed-form-ContentType-DisplayId') {
  13. // If date filter set change if to select list of years.
  14. if (isset($form['field_date_value'])) {
  15. // Get options from cache.
  16. $options = &drupal_static(__FUNCTION__);
  17. if (is_null($options)) {
  18. // Set up cid.
  19. $cid = 'myModule:ContentType:year';
  20. // Get data from cache.
  21. $data = \Drupal::cache()->get($cid);
  22. if (!$data) {
  23. $options[''] = new TranslatableMarkup('Year');
  24. $query = \Drupal::entityQuery('node');
  25. $query->condition('type', 'ContentType')
  26. ->condition('status', 1)
  27. ->sort('field_date', 'ASC');
  28. $result = $query->execute();
  29. if ($result) {
  30. $nodes = Node::loadMultiple($result);
  31. foreach ($nodes as $key => $node) {
  32. if ($node->hasField('field_date')) {
  33. $date = $node->field_date->value;
  34. if ($date) {
  35. $date = new DrupalDateTime($date, new DateTimeZone('UTC'));
  36. $year = $date->format('Y');
  37. if (!isset($options[$year])) {
  38. $options['.*' . $year] = $year;
  39. }
  40. }
  41. }
  42. }
  43. }
  44. // Cache tag.
  45. $cache_tags = ['node:ContentType:year'];
  46. // Set cache.
  47. \Drupal::cache()
  48. ->set($cid, $options, CacheBackendInterface::CACHE_PERMANENT, $cache_tags);
  49. }
  50. else {
  51. $options = $data->data;
  52. }
  53. }
  54. // Change the filter to select.
  55. $form['field_date_value'] = [
  56. '#type' => 'select',
  57. '#options' => $options,
  58. '#size' => NULL,
  59. '#default_value' => '',
  60. ];
  61. }
  62. }
  63. }
  64.  
  65. /**
  66. * Implements hook_ENTITY_TYPE_presave().
  67. */
  68. function myModule_node_presave(EntityInterface $entity) {
  69. $bundle = $entity->bundle();
  70. if ($bundle == 'ContentType') {
  71. // Check if a ContentType updated has a new year, and invalidate the.
  72. // options cached used in the custom views filter for filtering by year.
  73. $cid = 'myModule:ContentType:year';
  74. $data = \Drupal::cache()->get($cid);
  75. if ($data) {
  76. if ($entity->hasField('field_date')) {
  77. $options = $data->data;
  78. $date = $entity->field_date->value;
  79. if ($date) {
  80. $date = new DrupalDateTime($date, new DateTimeZone('UTC'));
  81. $year = $date->format('Y');
  82. if (!isset($options['.*' . $year])) {
  83. Cache::invalidateTags(['node:ContentType:year']);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement