View source
<?php
namespace Drupal\flexiform\Plugin\FlexiformFormEntity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\flexiform\FormEntity\FlexiformFormEntityBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FlexiformFormEntityProvided extends FlexiformFormEntityBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $entityBundleInfo;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_bundle_info) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityBundleInfo = $entity_bundle_info;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'));
}
public function getEntity() {
if (isset($this->configuration['entity']) && $this->configuration['entity'] instanceof EntityInterface) {
return $this->configuration['entity'];
}
}
public function getEntityType() {
return $this->configuration['entity_type'];
}
public function getBundle() {
return $this->configuration['bundle'];
}
public function configurationForm(array $form, FormStateInterface $form_state) {
$form = parent::configurationForm($form, $form_state);
$entity_type_options = $bundle_options = [];
foreach ($this->entityTypeManager
->getDefinitions() as $id => $entity_type) {
if ($entity_type
->isSubclassOf('\\Drupal\\Core\\Entity\\FieldableEntityInterface')) {
$entity_type_options[$id] = $entity_type
->getLabel();
}
}
$form['entity_type'] = [
'#type' => 'select',
'#options' => $entity_type_options,
'#title' => $this
->t('Entity Type'),
'#description' => $this
->t('The entity type.'),
'#default_value' => !empty($this->configuration['entity_type']) ? $this->configuration['entity_type'] : '',
'#ajax' => [
'wrapper' => 'bundle-select-wrapper',
'callback' => [
$this,
'ajaxBundleElementCallback',
],
],
];
$entity_type_id = $form['entity_type']['#default_value'];
if ($form_state
->getValue('bundle')) {
$entity_type_id = $form_state
->getValue('bundle');
}
if ($input = $form_state
->getUserInput()) {
$entity_type_id = $input['configuration']['entity_type'] ?: NULL;
}
if (!empty($entity_type_id)) {
foreach ($this->entityBundleInfo
->getBundleInfo($entity_type_id) as $bundle => $info) {
$bundle_options[$bundle] = $info['label'];
}
}
$form['bundle'] = [
'#prefix' => '<div id="bundle-select-wrapper">',
'#suffix' => '</div>',
'#type' => 'select',
'#options' => $bundle_options,
'#title' => $this
->t('Bundle'),
'#description' => $this
->t('The bundle.'),
'#default_value' => !empty($this->configuration['bundle']) ? $this->configuration['bundle'] : '',
];
return $form;
}
public function ajaxBundleElementCallback(array $form, FormStateInterface $form_state) {
return $form['configuration']['bundle'];
}
}