ViewStorageController.php

Definition of Drupal\views\ViewStorageController.

Namespace

Drupal\views

File

drupal/core/modules/views/lib/Drupal/views/ViewStorageController.php
View source
<?php

/**
 * @file
 * Definition of Drupal\views\ViewStorageController.
 */
namespace Drupal\views;

use Drupal\Core\Config\Entity\ConfigStorageController;
use Drupal\Core\Entity\EntityInterface;

/**
 * Defines the storage controller class for View entities.
 */
class ViewStorageController extends ConfigStorageController {

  /**
   * Overrides Drupal\config\ConfigStorageController::load();
   */
  public function load(array $ids = NULL) {
    $entities = parent::load($ids);

    // Only return views for enabled modules.
    return array_filter($entities, function ($entity) {
      if (module_exists($entity
        ->get('module'))) {
        return TRUE;
      }
      return FALSE;
    });
  }

  /**
   * Overrides Drupal\config\ConfigStorageController::attachLoad();
   */
  protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
    foreach ($queried_entities as $id => $entity) {
      $this
        ->attachDisplays($entity);
    }
    parent::attachLoad($queried_entities, $revision_id);
  }

  /**
   * Overrides Drupal\config\ConfigStorageController::postSave().
   */
  public function postSave(EntityInterface $entity, $update) {
    parent::postSave($entity, $update);

    // Clear caches.
    views_invalidate_cache();
  }

  /**
   * Overrides Drupal\config\ConfigStorageController::create().
   */
  public function create(array $values) {

    // If there is no information about displays available add at least the
    // default display.
    $values += array(
      'display' => array(
        'default' => array(
          'display_plugin' => 'default',
          'id' => 'default',
          'display_title' => 'Master',
          'position' => 0,
          'display_options' => array(),
        ),
      ),
    );
    $entity = parent::create($values);
    $this
      ->attachDisplays($entity);
    return $entity;
  }

  /**
   * Add defaults to the display options.
   *
   * @param Drupal\Core\Entity\EntityInterface $entity
   */
  protected function attachDisplays(EntityInterface $entity) {
    if (isset($entity->display) && is_array($entity->display)) {
      $displays = array();
      foreach ($entity
        ->get('display') as $key => $options) {
        $options += array(
          'display_options' => array(),
          'display_plugin' => NULL,
          'id' => NULL,
          'display_title' => '',
          'position' => NULL,
        );

        // Add the defaults for the display.
        $displays[$key] = $options;
      }
      $entity
        ->set('display', $displays);
    }
  }

  /**
   * Overrides Drupal\config\ConfigStorageController::getProperties();
   */
  protected function getProperties(EntityInterface $entity) {
    $names = array(
      'api_version',
      'base_field',
      'base_table',
      'core',
      'description',
      'disabled',
      'display',
      'human_name',
      'module',
      'name',
      'tag',
      'uuid',
    );
    $properties = array();
    foreach ($names as $name) {
      $properties[$name] = $entity
        ->get($name);
    }
    return $properties;
  }

}

Classes

Namesort descending Description
ViewStorageController Defines the storage controller class for View entities.