SiteSettingEntityForm.php in Site Settings and Labels 8
File
src/Form/SiteSettingEntityForm.php
View source
<?php
namespace Drupal\site_settings\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\site_settings\Entity\SiteSettingEntityType;
use Drupal\site_settings\SiteSettingsLoader;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SiteSettingEntityForm extends ContentEntityForm {
protected $siteSettingsLoader;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, SiteSettingsLoader $site_settings_loader) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->siteSettingsLoader = $site_settings_loader;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('site_settings.loader'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$site_settings_entity_type = SiteSettingEntityType::load($this->entity
->getType());
$form['heading1'] = [
'#markup' => '<h2>' . $site_settings_entity_type
->get('label') . '</h2>',
'#weight' => -100,
];
$form['name']['widget'][0]['value']['#value'] = $site_settings_entity_type
->get('label');
$form['fieldset']['widget'][0]['value']['#value'] = $site_settings_entity_type
->get('fieldset');
hide($form['name']);
hide($form['user_id']);
hide($form['fieldset']);
if (isset($form['multiple'])) {
hide($form['multiple']);
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$entity = $this->entity;
$entity_bundle = $entity
->bundle();
$entity_type = $entity
->getEntityType()
->getBundleEntityType();
$query = $this->entityTypeManager
->getStorage('site_setting_entity')
->getQuery();
$query
->condition('type', $entity_bundle);
$existing = $query
->execute();
$bundle = $this->entityTypeManager
->getStorage($entity_type)
->load($entity_bundle);
if (!$bundle->multiple) {
if (count($existing) > 0 && $entity
->id() != reset($existing)) {
$form_state
->setErrorByName('name', $this
->t('There can only be one of this setting.'));
}
}
}
public function save(array $form, FormStateInterface $form_state) {
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addMessage($this
->t('Created the %label Site Setting.', [
'%label' => $this->entity
->label(),
]));
break;
default:
$this
->messenger()
->addMessage($this
->t('Saved the %label Site Setting.', [
'%label' => $this->entity
->label(),
]));
}
$this->siteSettingsLoader
->clearCache();
$form_state
->setRedirect('entity.site_setting_entity.collection');
}
}