ViewStorageController.php 3.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

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

namespace Drupal\views;

use Drupal\config\ConfigStorageController;
11
use Drupal\entity\StorableInterface;
12 13 14 15 16 17 18 19

class ViewStorageController extends ConfigStorageController {

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

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  /**
   * Overrides Drupal\config\ConfigStorageController::save().
   *
   * This currently replaces the reflection code with a static array of
   * properties to be set on the config object. This can be removed
   * when the view storage is isolated so the ReflectionClass can work.
   */
  public function save(StorableInterface $entity) {
    $prefix = $this->entityInfo['config prefix'] . '.';

    // Load the stored entity, if any.
    if ($entity->getOriginalID()) {
      $id = $entity->getOriginalID();
    }
    else {
      $id = $entity->id();
    }
    $config = config($prefix . $id);
    $config->setName($prefix . $entity->id());

    if (!$config->isNew() && !isset($entity->original)) {
      $entity->original = entity_load_unchanged($this->entityType, $id);
    }

    $this->preSave($entity);
    $this->invokeHook('presave', $entity);

51
    // @todo: This temp measure will be removed once we have a better way or
52
    // separation of storage and the executed view.
53
    $config_properties = array (
54 55 56 57 58 59 60 61 62 63 64
      'disabled',
      'api_version',
      'name',
      'description',
      'tag',
      'base_table',
      'human_name',
      'core',
      'display',
    );

65 66 67 68
    foreach ($config_properties as $property) {
      if ($property == 'display') {
        $displays = array();
        foreach ($entity->display as $key => $display) {
69 70 71 72 73 74 75
          $displays[$key] = array(
            'display_options' => $display->display_options,
            'display_plugin' => $display->display_plugin,
            'id' => $display->id,
            'display_title' => $display->display_title,
            'position' => isset($display->position) ? $display->position : 0,
          );
76 77 78 79 80 81
        }
        $config->set('display', $displays);
      }
      else {
        $config->set($property, $entity->$property);
      }
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    }

    if (!$config->isNew()) {
      $return = SAVED_NEW;
      $config->save();
      $this->postSave($entity, TRUE);
      $this->invokeHook('update', $entity);
    }
    else {
      $return = SAVED_UPDATED;
      $config->save();
      $entity->enforceIsNew(FALSE);
      $this->postSave($entity, FALSE);
      $this->invokeHook('insert', $entity);
    }

    unset($entity->original);

    return $return;
  }

103 104 105 106 107 108 109 110 111 112
  /**
   * Overrides Drupal\config\ConfigStorageController::create().
   */
  public function create(array $values) {
    $entity = parent::create($values);
    $this->attachDisplays($entity);
    return $entity;
  }

  /**
113
   * Attaches an array of ViewDisplay objects to the view display property.
damiankloip's avatar
damiankloip committed
114 115
   *
   * @param Drupal\entity\StorableInterface $entity
116
   */
117
  protected function attachDisplays($entity) {
118
    if (isset($entity->display) && is_array($entity->display)) {
119 120
      $displays = array();
      foreach ($entity->get('display') as $key => $options) {
121 122
        // Create a ViewDisplay object using the display options.
        $displays[$key] = new ViewDisplay($options);
123
      }
124
      $entity->set('display', $displays);
125 126 127
    }
  }

128
}