You are here

trait MicrositePluginTrait in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 modules/entity_hierarchy_microsite/src/Plugin/MicrositePluginTrait.php \Drupal\entity_hierarchy_microsite\Plugin\MicrositePluginTrait

Defines a trait for microsite plugin functionality.

Hierarchy

3 files declare their use of MicrositePluginTrait
ChildOfMicrosite.php in modules/entity_hierarchy_microsite/src/Plugin/Condition/ChildOfMicrosite.php
MicrositeLogoBranding.php in modules/entity_hierarchy_microsite/src/Plugin/Block/MicrositeLogoBranding.php
MicrositeMenu.php in modules/entity_hierarchy_microsite/src/Plugin/Block/MicrositeMenu.php

File

modules/entity_hierarchy_microsite/src/Plugin/MicrositePluginTrait.php, line 13

Namespace

Drupal\entity_hierarchy_microsite\Plugin
View source
trait MicrositePluginTrait {

  /**
   * Child of microsite lookup.
   *
   * @var \Drupal\entity_hierarchy_microsite\ChildOfMicrositeLookupInterface
   */
  protected $childOfMicrositeLookup;

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

  /**
   * Sets value of NestedSetStorageFactory.
   *
   * @param \Drupal\entity_hierarchy_microsite\ChildOfMicrositeLookupInterface $childOfMicrositeLookup
   *   Lookup.
   *
   * @return $this
   */
  public function setChildOfMicrositeLookup(ChildOfMicrositeLookupInterface $childOfMicrositeLookup) {
    $this->childOfMicrositeLookup = $childOfMicrositeLookup;
    return $this;
  }

  /**
   * Entity field manager.
   *
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
   *   Entity field manager.
   *
   * @return $this
   */
  protected function setEntityFieldManager(EntityFieldManagerInterface $entityFieldManager) {
    $this->entityFieldManager = $entityFieldManager;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = new static($configuration, $plugin_id, $plugin_definition);
    return $instance
      ->setChildOfMicrositeLookup($container
      ->get('entity_hierarchy_microsite.microsite_lookup'))
      ->setEntityFieldManager($container
      ->get('entity_field.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'field' => '',
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Entity hierarchy field'),
      '#options' => $this
        ->getFieldOptions(),
      '#empty_option' => 'None',
      '#empty_value' => '',
      '#default_value' => $this->configuration['field'],
      '#description' => $this
        ->t('Select the field to use to identify if the current node is the child of a microsite'),
    ];
    return parent::buildConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['field'] = $form_state
      ->getValue('field');
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * Gets field options.
   *
   * @return array
   *   Field names keyed by label.
   */
  protected function getFieldOptions() {
    $fields = $this->entityFieldManager
      ->getFieldMapByFieldType('entity_reference_hierarchy');
    $options = [];
    if (isset($fields['node'])) {
      foreach ($fields['node'] as $field_name => $detail) {
        foreach ($detail['bundles'] as $bundle) {

          /** @var \Drupal\Core\Field\FieldDefinitionInterface $field */
          $field = $this->entityFieldManager
            ->getFieldDefinitions('node', $bundle)[$field_name];
          $options[$field_name] = $field
            ->getLabel();
        }
      }
    }
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MicrositePluginTrait::$childOfMicrositeLookup protected property Child of microsite lookup.
MicrositePluginTrait::$entityFieldManager protected property Entity field manager.
MicrositePluginTrait::buildConfigurationForm public function
MicrositePluginTrait::create public static function
MicrositePluginTrait::defaultConfiguration public function
MicrositePluginTrait::getFieldOptions protected function Gets field options.
MicrositePluginTrait::setChildOfMicrositeLookup public function Sets value of NestedSetStorageFactory.
MicrositePluginTrait::setEntityFieldManager protected function Entity field manager.
MicrositePluginTrait::submitConfigurationForm public function