You are here

abstract class PanelizerEntityBase in Panelizer 8.5

Same name and namespace in other branches
  1. 8.3 src/Plugin/PanelizerEntityBase.php \Drupal\panelizer\Plugin\PanelizerEntityBase
  2. 8.4 src/Plugin/PanelizerEntityBase.php \Drupal\panelizer\Plugin\PanelizerEntityBase

Base class for Panelizer entity plugins.

Hierarchy

Expanded class hierarchy of PanelizerEntityBase

3 files declare their use of PanelizerEntityBase
PanelizerNode.php in src/Plugin/PanelizerEntity/PanelizerNode.php
PanelizerTerm.php in src/Plugin/PanelizerEntity/PanelizerTerm.php
PanelizerUser.php in src/Plugin/PanelizerEntity/PanelizerUser.php

File

src/Plugin/PanelizerEntityBase.php, line 18

Namespace

Drupal\panelizer\Plugin
View source
abstract class PanelizerEntityBase extends PluginBase implements PanelizerEntityInterface, ContainerFactoryPluginInterface {
  use StringTranslationTrait;

  /**
   * @var \Drupal\Panels\PanelsDisplayManager
   */
  protected $panelsManager;

  /**
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Panels\PanelsDisplayManager $panels_manager
   *   The Panels display manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, PanelsDisplayManager $panels_manager, EntityFieldManagerInterface $entity_field_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->panelsManager = $panels_manager;
    $this->entityFieldManager = $entity_field_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('panels.display_manager'), $container
      ->get('entity_field.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultDisplay(EntityViewDisplayInterface $display, $bundle, $view_mode) {
    $panels_display = $this->panelsManager
      ->createDisplay();
    $panels_display
      ->setConfiguration([
      'label' => $this
        ->t('Default'),
    ] + $panels_display
      ->getConfiguration());
    $panels_display
      ->setLayout('layout_onecol');

    // @todo: For now we always use the IPE, but we should support not using the ipe.
    $panels_display
      ->setBuilder('ipe');
    $panels_display
      ->setPattern('panelizer');

    // Add all the visible fields to the Panel.
    $entity_type_id = $this
      ->getPluginId();

    /**
     * @var string $field_name
     * @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition
     */
    foreach ($this->entityFieldManager
      ->getFieldDefinitions($entity_type_id, $bundle) as $field_name => $field_definition) {

      // Skip the Panelizer field.
      if ($field_definition
        ->getType() == 'panelizer') {
        continue;
      }
      if ($component = $display
        ->getComponent($field_name)) {
        $weight = $component['weight'];
        unset($component['weight']);
        $panels_display
          ->addBlock([
          'id' => 'entity_field:' . $entity_type_id . ':' . $field_name,
          'label' => $field_definition
            ->getLabel(),
          'provider' => 'ctools_block',
          'label_display' => '0',
          'formatter' => $component,
          'context_mapping' => [
            'entity' => '@panelizer.entity_context:entity',
          ],
          'region' => 'content',
          'weight' => $weight,
        ]);
      }
    }
    return $panels_display;
  }

  /**
   * {@inheritdoc}
   */
  public function alterBuild(array &$build, EntityInterface $entity, PanelsDisplayVariant $panels_display, $view_mode) {

    // By default, do nothing!
  }

  /**
   * {@inheritdoc}
   */
  public function preprocessViewMode(array &$variables, EntityInterface $entity, PanelsDisplayVariant $panels_display, $view_mode) {
    $entity_type_id = $this
      ->getPluginId();

    // Add some default classes.
    $variables['attributes']['class'][] = $entity_type_id;
    $variables['attributes']['class'][] = $entity_type_id . '--type-' . $entity
      ->bundle();
    $variables['attributes']['class'][] = $entity_type_id . '--view-mode-' . $view_mode;
    $variables['attributes']['class'][] = 'clearfix';

    // Don't render the title in the template
    if ($view_mode == 'full') {
      $variables['title'] = '';
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PanelizerEntityBase::$entityFieldManager protected property
PanelizerEntityBase::$panelsManager protected property
PanelizerEntityBase::alterBuild public function Alter the built entity view in an entity specific way before rendering. Overrides PanelizerEntityInterface::alterBuild 3
PanelizerEntityBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
PanelizerEntityBase::getDefaultDisplay public function Creates a default Panels display from the core Entity display. Overrides PanelizerEntityInterface::getDefaultDisplay 3
PanelizerEntityBase::preprocessViewMode public function Preprocess the variables for the view mode template. Overrides PanelizerEntityInterface::preprocessViewMode 1
PanelizerEntityBase::__construct public function Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.