View source
<?php
namespace Drupal\apigee_edge\Form;
use Drupal\apigee_edge\Entity\ListBuilder\EdgeEntityListBuilder;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\BaseFormIdInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EdgeEntityDisplaySettingsForm extends ConfigFormBase implements BaseFormIdInterface {
protected $entityTypeManager;
protected $moduleHandler;
protected $entityDisplayRepository;
protected $entityTypeId;
protected $routeMatch;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository, ModuleHandlerInterface $module_handler, RouteMatchInterface $route_match) {
parent::__construct($config_factory);
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->entityDisplayRepository = $entity_display_repository;
$this->routeMatch = $route_match;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('entity_display.repository'), $container
->get('module_handler'), $container
->get('current_route_match'));
}
public function getBaseFormId() {
return 'apigee_edge_display_settings_form';
}
public function getFormId() {
$entity_type_id = $this->routeMatch
->getParameter('entity_type_id');
return "apigee_edge_display_settings_form.{$entity_type_id}";
}
protected function getEditableConfigNames() {
return [
'apigee_edge.display_settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
$this->entityTypeId = $entity_type_id;
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$config = $this
->configFactory()
->get("apigee_edge.display_settings.{$entity_type_id}");
$form['display_settings'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Configure the display for @label listing page.', [
'@label' => $entity_type
->getPluralLabel(),
]),
'#collapsible' => FALSE,
];
$form['display_settings']['display_type'] = [
'#type' => 'select',
'#title' => $this
->t('Type'),
'#default_value' => $config
->get('display_type'),
'#required' => TRUE,
'#description' => $this
->t('Select the <em>Default</em> type to display a table of entities with links to entity operations. Select <em>Display mode</em> to configure a custom display.'),
'#options' => [
EdgeEntityListBuilder::DEFAULT_DISPLAY_TYPE => $this
->t('Default'),
EdgeEntityListBuilder::VIEW_MODE_DISPLAY_TYPE => $this
->t('Display mode'),
],
];
$form['display_settings']['display_mode_container'] = [
'#type' => 'container',
'#states' => [
'visible' => [
':input[name="display_type"]' => [
'value' => 'view_mode',
],
],
],
];
$display_modes = [
'default' => $this
->t('Default'),
];
foreach ($this->entityDisplayRepository
->getViewModes($entity_type
->id()) as $name => $view_mode) {
$display_modes[$name] = $view_mode['label'];
}
$form['display_settings']['display_mode_container']['view_mode'] = [
'#type' => 'select',
'#title' => $this
->t('Display mode'),
'#default_value' => $config
->get('view_mode'),
'#description' => $this
->t('Select the display mode.', [
':uri' => '#',
]),
'#options' => $display_modes,
];
if ($this->moduleHandler
->moduleExists('field_ui')) {
$form['display_settings']['display_mode_container']['display_mode_help'] = [
'#theme' => 'item_list',
'#items' => [
[
'#markup' => $this
->t('<a href=":uri">Click here</a> to configure the display.', [
':uri' => Url::fromRoute("entity.entity_view_display.{$entity_type->id()}.default")
->toString(),
]),
],
[
'#markup' => $this
->t('<a href=":uri">Click here</a> to add a new display mode.', [
':uri' => Url::fromRoute('entity.entity_view_mode.collection')
->toString(),
]),
],
],
];
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->configFactory()
->getEditable("apigee_edge.display_settings.{$this->entityTypeId}")
->set('display_type', $form_state
->getValue('display_type'))
->set('view_mode', $form_state
->getValue('view_mode'))
->save();
parent::submitForm($form, $form_state);
}
}