MicrositePluginTrait.php in Entity Reference Hierarchy 3.x
File
modules/entity_hierarchy_microsite/src/Plugin/MicrositePluginTrait.php
View source
<?php
namespace Drupal\entity_hierarchy_microsite\Plugin;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_hierarchy_microsite\ChildOfMicrositeLookupInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
trait MicrositePluginTrait {
protected $childOfMicrositeLookup;
protected $entityFieldManager;
public function setChildOfMicrositeLookup(ChildOfMicrositeLookupInterface $childOfMicrositeLookup) {
$this->childOfMicrositeLookup = $childOfMicrositeLookup;
return $this;
}
protected function setEntityFieldManager(EntityFieldManagerInterface $entityFieldManager) {
$this->entityFieldManager = $entityFieldManager;
return $this;
}
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'));
}
public function defaultConfiguration() {
return [
'field' => '',
] + parent::defaultConfiguration();
}
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);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['field'] = $form_state
->getValue('field');
parent::submitConfigurationForm($form, $form_state);
}
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) {
$field = $this->entityFieldManager
->getFieldDefinitions('node', $bundle)[$field_name];
$options[$field_name] = $field
->getLabel();
}
}
}
return $options;
}
}