Skip to content
View.php 9.7 KiB
Newer Older
 * Definition of Drupal\views\Entity\View.
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\views\ViewStorageInterface;
use Drupal\views\ViewExecutable;
 * Defines a View configuration entity class.
 *
 *   id = "view",
 *   label = @Translation("View"),
 *     "access" = "Drupal\views\ViewAccessController"
 *   admin_permission = "administer views",
class View extends ConfigEntityBase implements ViewStorageInterface {
  /**
   * The name of the base table this view will use.
   *
   * @var string
   */
  /**
   * The description of the view, which is used only in the interface.
   *
   * @var string
   */

  /**
   * The "tags" of a view.
   *
   * The tags are stored as a single string, though it is used as multiple tags
   * for example in the views overview.
   *
   * @var string
   */

  /**
   * The core version the view was created for.
   *
   * @var int
   */
  protected $core = \Drupal::CORE_COMPATIBILITY;

  /**
   * Stores all display handlers of this view.
   *
   * An array containing Drupal\views\Plugin\views\display\DisplayPluginBase
   * objects.
   *
   * @var array
   */
   * Stores a reference to the executable version of this view.
   * Gets an executable instance for this view.
   *
   * @return \Drupal\views\ViewExecutable
   *   A view executable instance.
    // Ensure that an executable View is available.
    if (!isset($this->executable)) {
      $this->executable = Views::executableFactory()->get($this);
  /**
   * Overrides Drupal\Core\Config\Entity\ConfigEntityBase::createDuplicate().
   */
  public function createDuplicate() {
    $duplicate = parent::createDuplicate();
    unset($duplicate->executable);
    return $duplicate;
  }

   * Overrides \Drupal\Core\Entity\Entity::label().
   * When a certain view doesn't have a label return the ID.
    if (!$label = $this->get('label')) {
      $label = $this->id();
   * Adds a new display handler to the view, automatically creating an ID.
   * @param string $plugin_id
   *   (optional) The plugin type from the Views plugin annotation. Defaults to
   *   'page'.
   * @param string $title
   *   (optional) The title of the display. Defaults to NULL.
   * @param string $id
   *   (optional) The ID to use, e.g., 'default', 'page_1', 'block_2'. Defaults
   *   to NULL.
   *
   * @return string|false
   *   The key to the display in $view->display, or FALSE if no plugin ID was
   *   provided.
  public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
    if (empty($plugin_id)) {
    $plugin = Views::pluginManager('display')->getDefinition($plugin_id);
    if (empty($plugin)) {
      $plugin['title'] = t('Broken');
    }

    if (empty($id)) {
      $id = $this->generateDisplayId($plugin_id);
      // Generate a unique human-readable name by inspecting the counter at the
      // end of the previous display ID, e.g., 'page_1'.
      if ($id !== 'default') {
        preg_match("/[0-9]+/", $id, $count);
        $count = $count[0];
      }
      else {
        $count = '';
      }

      if (empty($title)) {
        // If there is no title provided, use the plugin title, and if there are
        // multiple displays, append the count.
        $title = $plugin['title'];
    $display_options = array(
      'display_plugin' => $plugin_id,
      'id' => $id,
      'display_title' => $title,
      'provider' => $plugin['provider'],
    // Add the display options to the view.
    $this->display[$id] = $display_options;
   * Generates a display ID of a certain plugin type.
   * @param string $plugin_id
   *   Which plugin should be used for the new display ID.
  protected function generateDisplayId($plugin_id) {
    // 'default' is singular and is unique, so just go with 'default'
    // for it. For all others, start counting.
    $count = 1;

    // Loop through IDs based upon our style plugin name until
    // we find one that is unused.
    while (!empty($this->display[$id])) {
   */
  public function &getDisplay($display_id) {
    return $this->display[$display_id];
  }

    $names = array(
      'base_field',
      'base_table',
      'core',
      'description',
    );
    $properties = array();
    foreach ($names as $name) {
      $properties[$name] = $this->get($name);
    }
    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();

    // Ensure that the view is dependant on the module that implements the view.
    $this->addDependency('module', $this->module);
    // Ensure that the view is dependant on the module that provides the schema
    // for the base table.
    $schema = drupal_get_schema($this->base_table);
    if ($this->module != $schema['module']) {
      $this->addDependency('module', $schema['module']);
    }

    $handler_types = array();
    foreach (ViewExecutable::viewsHandlerTypes() as $type) {
      $handler_types[] = $type['plural'];
    }
    foreach ($this->get('display') as $display) {
      foreach ($handler_types as $handler_type) {
        if (!empty($display['display_options'][$handler_type])) {
          foreach ($display['display_options'][$handler_type] as $handler) {
            if (isset($handler['provider']) && empty($handler['optional'])) {
              $this->addDependency('module', $handler['provider']);
            }
          }
        }
      }
    }
    return $this->dependencies;
  }

  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) {
    parent::postSave($storage_controller, $update);

    // Clear cache tags for this view.
    // @todo Remove if views implements a view_builder controller.
    $id = $this->id();
    Cache::deleteTags(array('view' => array($id => $id)));
   */
  public static function postLoad(EntityStorageControllerInterface $storage_controller, array &$entities) {
    parent::postLoad($storage_controller, $entities);
    foreach ($entities as $entity) {
      $entity->mergeDefaultDisplaysOptions();
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function preCreate(EntityStorageControllerInterface $storage_controller, array &$values) {
    parent::preCreate($storage_controller, $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(),
        ),
      )
    );
  }

  /**
   * {@inheritdoc}
   */
  public function postCreate(EntityStorageControllerInterface $storage_controller) {
  /**
   * {@inheritdoc}
   */
  public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) {
    parent::postDelete($storage_controller, $entities);

    $tempstore = \Drupal::service('user.tempstore')->get('views');
      $id = $entity->id();
      $tempstore->delete($id);
      $tags['view'][$id] = $id;

    // Clear cache tags for these views.
    // @todo Remove if views implements a view_builder controller.
    Cache::deleteTags($tags);
  /**
   * {@inheritdoc}
   */
  public function mergeDefaultDisplaysOptions() {
    $displays = array();
    foreach ($this->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;
    }
    // Sort the displays.
    uasort($displays, function ($display1, $display2) {
      if ($display1['position'] != $display2['position']) {
        return $display1['position'] < $display2['position'] ? -1 : 1;
      }
      return 0;
    });
    $this->set('display', $displays);
  }