You are here

cacheflush_ui.module in CacheFlush 8

Same filename and directory in other branches
  1. 7.3 modules/cacheflush_ui/cacheflush_ui.module

Cacheflush User Interface.

File

modules/cacheflush_ui/cacheflush_ui.module
View source
<?php

/**
 * @file
 * Cacheflush User Interface.
 */
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\cacheflush_ui\CacheflushUIConstantsInterface;

/**
 * Implements hook_entity_type_alter().
 */
function cacheflush_ui_entity_type_alter(&$entity_info) {
  $entity_info['cacheflush']
    ->set('field_ui_base_route', 'entity.cacheflush.collection');
  $entity_info['cacheflush']
    ->setHandlerClass("access", 'Drupal\\cacheflush_ui\\CacheflushEntityAccessControlHandler');
  $entity_info['cacheflush']
    ->setHandlerClass("views_data", 'Drupal\\cacheflush_ui\\Entity\\CacheflushEntityViewsData');
  $entity_info['cacheflush']
    ->setHandlerClass("list_builder", 'Drupal\\cacheflush_ui\\CacheflushEntityListBuilder');
  $entity_info['cacheflush']
    ->setFormClass('default', 'Drupal\\cacheflush_ui\\Entity\\Form\\CacheflushEntityForm');
  $entity_info['cacheflush']
    ->setFormClass('add', 'Drupal\\cacheflush_ui\\Entity\\Form\\CacheflushEntityForm');
  $entity_info['cacheflush']
    ->setFormClass('edit', 'Drupal\\cacheflush_ui\\Entity\\Form\\CacheflushEntityForm');
  $entity_info['cacheflush']
    ->setFormClass('delete', 'Drupal\\cacheflush_ui\\Entity\\Form\\CacheflushEntityDeleteForm');
  $entity_info['cacheflush']
    ->setLinkTemplate('canonical', '/cacheflush/{cacheflush}');
  $entity_info['cacheflush']
    ->setLinkTemplate('edit-form', '/cacheflush/{cacheflush}/edit');
  $entity_info['cacheflush']
    ->setLinkTemplate('delete-form', '/cacheflush/{cacheflush}/delete');
}

/**
 * Implements hook_entity_base_field_info().
 */
function cacheflush_ui_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type
    ->id() == 'cacheflush') {
    $fields = [];
    $fields['menu'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Menu entry'))
      ->setDescription(t('Menu for Cacheflush entity.'))
      ->setDefaultValue(0);
    return $fields;
  }
}

/**
 * Implements hook_cacheflush_ui_tabs().
 */
function cacheflush_ui_cacheflush_ui_tabs() {
  $tabs = [
    'vertical_tabs_core' => [
      'name' => t('Core cache tables'),
      'validation' => 'cacheflush_ui_tab_validation',
      'weight' => -100,
    ],
    'vertical_tabs_functions' => [
      'name' => t('Other core cache options'),
      'validation' => 'cacheflush_ui_tab_validation',
      'weight' => -90,
    ],
    'vertical_tabs_custom' => [
      'name' => t('Contrib cache tables'),
      'validation' => 'cacheflush_ui_tab_validation',
      'weight' => -80,
    ],
    'vertical_tabs_often' => [
      'name' => t('Other contrib cache options'),
      'validation' => 'cacheflush_ui_tab_validation',
      'weight' => -70,
    ],
  ];
  return $tabs;
}

/**
 * Callback for tab validation.
 */
function cacheflush_ui_tab_validation($tab, &$form, &$form_state) {
  $values = $form_state
    ->getValue($tab);
  if ($values) {
    $storage = $form_state
      ->getStorage();
    foreach ($values as $preset => $value) {
      if ($value) {
        $storage['presets'][$preset]['functions'] = $storage['preset_options'][$preset]['functions'];
        $form_state
          ->setStorage($storage);
      }
    }
  }
}

/**
 * Implements hook_menu_links_discovered_alter().
 */
function cacheflush_ui_menu_links_discovered_alter(&$links) {
  $menus = cacheflush_load_multiple_by_properties([
    'status' => CacheflushUIConstantsInterface::CACHEFLUSH_PUBLISHED,
    'menu' => CacheflushUIConstantsInterface::CACHEFLUSH_MENU,
  ]);
  if ($menus) {
    foreach ($menus as $entity) {
      $links['cacheflush.presets.' . $entity
        ->id()] = [
        'title' => $entity
          ->getTitle(),
        'route_name' => 'cacheflush.presets.clear_id',
        'parent' => 'cacheflush.presets',
        'route_parameters' => [
          'cacheflush' => $entity
            ->id(),
        ],
      ];
    }
  }
}

/**
 * Implements hook_views_query_alter().
 */
function cacheflush_ui_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view
    ->id() == 'cacheflush_content') {
    $account = \Drupal::currentUser();
    if ($account
      ->hasPermission('cacheflush view any')) {
      foreach ($query->where as $key => &$condition_group) {
        foreach ($condition_group['conditions'] as &$condition) {
          if ($condition['field'] == 'cacheflush.uid = :cacheflush_uid ') {
            unset($query->where[$key]);
          }
        }
      }
    }
  }
}