You are here

class PanelsDisplayManager in Panels 8.3

Same name and namespace in other branches
  1. 8.4 src/PanelsDisplayManager.php \Drupal\panels\PanelsDisplayManager

A service that manages Panels displays.

Hierarchy

Expanded class hierarchy of PanelsDisplayManager

1 string reference to 'PanelsDisplayManager'
panels.services.yml in ./panels.services.yml
panels.services.yml
1 service uses PanelsDisplayManager
panels.display_manager in ./panels.services.yml
Drupal\panels\PanelsDisplayManager

File

src/PanelsDisplayManager.php, line 17

Namespace

Drupal\panels
View source
class PanelsDisplayManager implements PanelsDisplayManagerInterface {
  use SchemaCheckTrait {
    checkConfigSchema as private;
  }

  /**
   * @var \Drupal\Core\Display\VariantManager
   */
  protected $variantManager;

  /**
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
   */
  protected $typedConfigManager;

  /**
   * @param \Drupal\Core\Display\VariantManager $variant_manager
   */
  public function __construct(VariantManager $variant_manager, TypedConfigManagerInterface $typed_config_manager) {
    $this->variantManager = $variant_manager;
    $this->typedConfigManager = $typed_config_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function createDisplay($layout = NULL, $builder = NULL) {
    $display = $this->variantManager
      ->createInstance('panels_variant', []);

    // Set the default builder and layout.
    // @todo: load the defaults from config somewhere.
    $display
      ->setLayout($layout ?: 'onecol');
    $display
      ->setBuilder($builder ?: 'standard');
    return $display;
  }

  /**
   * Validates the config against the schema.
   *
   * @param array $config
   *   The configuration data.
   *
   * @throws \Exception
   *   If the configuration doesn't validate.
   */
  protected function validate(array $config) {
    $this->configName = 'display_variant.plugin.panels_variant';
    $definition = $this->typedConfigManager
      ->getDefinition($this->configName);
    $data_definition = $this->typedConfigManager
      ->buildDataDefinition($definition, $config);
    $this->schema = $this->typedConfigManager
      ->create($data_definition, $config);
    $errors = array();
    foreach ($config as $key => $value) {
      $errors = array_merge($errors, $this
        ->checkValue($key, $value));
    }
    if (!empty($errors)) {
      $error_list = [];
      foreach ($errors as $key => $error) {
        $error_list[] = $key . ': ' . $error;
      }
      throw new \Exception("Config for Panels display doesn't validate: " . implode(', ', $error_list));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function importDisplay(array $config, $validate = TRUE) {

    // Validate against the schema if requested.
    if ($validate) {
      $this
        ->validate($config);
    }
    return $this->variantManager
      ->createInstance('panels_variant', $config);
  }

  /**
   * {@inheritdoc}
   */
  public function exportDisplay(PanelsDisplayVariant $display) {
    return $display
      ->getConfiguration();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PanelsDisplayManager::$typedConfigManager protected property
PanelsDisplayManager::$variantManager protected property
PanelsDisplayManager::createDisplay public function Create a new panels display. Overrides PanelsDisplayManagerInterface::createDisplay
PanelsDisplayManager::exportDisplay public function Export configuration from a panels display. Overrides PanelsDisplayManagerInterface::exportDisplay
PanelsDisplayManager::importDisplay public function Creates a panels display from exported configuration. Overrides PanelsDisplayManagerInterface::importDisplay
PanelsDisplayManager::validate protected function Validates the config against the schema.
PanelsDisplayManager::__construct public function
SchemaCheckTrait::$configName protected property The configuration object name under test.
SchemaCheckTrait::$schema protected property The config schema wrapper object for the configuration object under test.
SchemaCheckTrait::checkConfigSchema public function Checks the TypedConfigManager has a valid schema for the configuration.
SchemaCheckTrait::checkValue protected function Helper method to check data type.