View source
<?php
namespace Drupal\flexiform\Plugin\FormEnhancer;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\ctools\Form\AjaxFormTrait;
use Drupal\flexiform\FormEnhancer\ConfigurableFormEnhancerBase;
class MultipleEntityForm extends ConfigurableFormEnhancerBase {
use StringTranslationTrait;
use AjaxFormTrait;
protected $supportedEvents = [
'init_form_entity_config',
];
public function configurationForm(array $form, FormStateInterface $form_state) {
$target_entity_type = $this->formDisplay
->get('targetEntityType');
$target_entity_def = \Drupal::service('entity_type.manager')
->getDefinition($target_entity_type);
$url_params = [
'form_mode_name' => $this->formDisplay
->get('mode'),
];
if ($target_entity_def
->get('bundle_entity_type')) {
$url_params[$target_entity_def
->get('bundle_entity_type')] = $this->formDisplay
->get('bundle');
}
else {
if ($target_entity_def
->hasKey('bundle')) {
$url_params['bundle'] = $this->formDisplay
->get('bundle');
}
}
$form['add'] = [
'#type' => 'link',
'#title' => $this
->t('Add Entity'),
'#url' => Url::fromRoute("entity.entity_form_display.{$target_entity_type}.form_mode.form_entity_add", $url_params),
'#attributes' => $this
->getAjaxButtonAttributes(),
'#attached' => [
'library' => [
'core/drupal.ajax',
],
],
];
$form['entities'] = [
'#type' => 'table',
'#header' => [
$this
->t('Entity'),
$this
->t('Plugin'),
$this
->t('Operations'),
],
'#title' => t('Entities'),
'#empty' => t('This form display has no entities yet.'),
];
foreach ($this->formDisplay
->getFormEntityManager($form_state)
->getContexts() as $namespace => $context) {
$operations = [];
if (!empty($namespace)) {
$operation_params = $url_params;
$operation_params['entity_namespace'] = $namespace;
$operations['edit'] = [
'title' => $this
->t('Edit'),
'weight' => 10,
'url' => Url::fromRoute("entity.entity_form_display.{$target_entity_type}.form_mode.form_entity_edit", $operation_params),
'attributes' => $this
->getAjaxButtonAttributes(),
];
}
$form['entities'][$namespace] = [
'human_name' => [
'#plain_text' => $context
->getContextDefinition()
->getLabel(),
],
'plugin' => [
'#plain_text' => $context
->getFormEntity()
->getLabel(),
],
'operations' => [
'#type' => 'operations',
'#links' => $operations,
'#attached' => [
'library' => [
'core/drupal.ajax',
],
],
],
];
}
return $form;
}
public function initFormEntityConfig() {
return !empty($this->configuration['entities']) ? $this->configuration['entities'] : [];
}
public function getFormEntityConfig($namespace) {
return $this->configuration['entities'][$namespace] ?? FALSE;
}
public function addFormEntityConfig($namespace, $configuration) {
$this->configuration['entities'][$namespace] = $configuration;
return $this;
}
public function removeFormEntityConfig($namespace) {
unset($this->configuration['entities'][$namespace]);
return $this;
}
}