BaseFieldConfigFromBase.php in Apigee Edge 8
File
src/Form/BaseFieldConfigFromBase.php
View source
<?php
namespace Drupal\apigee_edge\Form;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class BaseFieldConfigFromBase extends FormBase {
protected $entityFieldManager;
protected $entityTypeManager;
public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_field.manager'), $container
->get('entity_type.manager'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$base_fields = $this->entityFieldManager
->getBaseFieldDefinitions($this
->entityType());
$form['#attached']['library'][] = 'apigee_edge/apigee_edge.admin';
$form['table'] = [
'#type' => 'table',
'#caption' => $this
->t('Base field settings'),
'#header' => [
$this
->t('Field name'),
$this
->t('Required'),
],
];
foreach ($base_fields as $name => $base_field) {
if ($base_field
->isDisplayConfigurable('form')) {
$form['table'][$name] = [
'name' => [
'#type' => 'item',
'#markup' => $base_field
->getLabel(),
],
'required' => [
'#type' => 'checkbox',
'#title' => $this
->t('Required'),
'#title_display' => 'invisible',
'#default_value' => $base_field
->isRequired(),
],
];
}
}
foreach ($this
->getLockedBaseFields() as $locked) {
$form['table'][$locked]['required']['#disabled'] = TRUE;
}
$form['save'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$display = $this->entityTypeManager
->getStorage('entity_form_display')
->load("{$this->entityType()}.{$this->entityType()}.default");
if ($display) {
foreach ($form_state
->getValue('table') as $name => $data) {
$component = $display
->getComponent($name);
if ($data['required'] && !($component && $component['region'] !== 'hidden')) {
$form_state
->setError($form['table'][$name]['required'], $this
->t('%field-name is hidden on the default form display.', [
'%field-name' => $form['table'][$name]['name']['#markup'],
]));
}
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$required_base_fields = [];
foreach ($form_state
->getValue('table') as $name => $data) {
if ($data['required']) {
$required_base_fields[] = $name;
}
}
$this
->saveRequiredBaseFields($required_base_fields);
drupal_flush_all_caches();
$this
->messenger()
->addStatus($this
->t('Field settings have been saved successfully.'));
}
protected abstract function entityType() : string;
protected abstract function getLockedBaseFields() : array;
protected abstract function saveRequiredBaseFields(array $required_base_fields) : void;
}