View source
<?php
namespace Drupal\config_pages;
use Drupal\Component\Utility\Html;
use Drupal\config_pages\Entity\ConfigPagesType;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Session\AccountProxyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\Core\Url;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Messenger\MessengerInterface;
class ConfigPagesForm extends ContentEntityForm {
protected $configPagesStorage;
protected $configPagesTypeStorage;
protected $languageManager;
protected $entity;
protected $user;
protected $messenger;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeManagerInterface $entity_type_manager, EntityStorageInterface $config_pages_storage, EntityStorageInterface $config_pages_type_storage, LanguageManagerInterface $language_manager, MessengerInterface $messenger, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountProxyInterface $user = NULL) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->configPagesStorage = $config_pages_storage;
$this->configPagesTypeStorage = $config_pages_type_storage;
$this->languageManager = $language_manager;
$this->entityTypeManager = $entity_type_manager;
$this->user = $user;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($container
->get('entity.repository'), $entity_type_manager, $entity_type_manager
->getStorage('config_pages'), $entity_type_manager
->getStorage('config_pages_type'), $container
->get('language_manager'), $container
->get('messenger'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('current_user'));
}
protected function prepareEntity() {
$config_pages = $this->entity;
$this->configPagesTypeStorage
->load($config_pages
->bundle());
}
public function form(array $form, FormStateInterface $form_state) {
$config_pages = $this->entity;
$config_pages_type = $this->configPagesTypeStorage
->load($config_pages
->bundle());
$form = parent::form($form, $form_state, $config_pages);
$conditions['type'] = $config_pages
->bundle();
$list = $this->entityTypeManager
->getStorage('config_pages')
->loadByProperties($conditions);
$show_warning = $config_pages_type->context['show_warning'];
$label = $config_pages_type
->getContextLabel();
if (!empty($label) && $show_warning) {
$this->messenger
->addWarning($this
->t('Please note that this Page is context sensitive, current context is %label', [
'%label' => $label,
]));
}
if ($this->operation == 'edit') {
$form['#title'] = $this
->t('Edit custom config page %label', [
'%label' => $config_pages
->label(),
]);
}
$form['#attributes']['class'][0] = 'config-page-' . Html::getClass($config_pages
->bundle()) . '-form';
$links = $config_pages_type
->getContextLinks();
foreach ($links as $group_id => $context_links) {
if (!count($context_links)) {
continue;
}
$form['context_selection_' . $group_id] = [
'#type' => 'fieldset',
'#title' => $this
->t('Choose @name context', [
'@name' => $group_id,
]),
'#weight' => -100,
];
foreach ($context_links as $pos => $link) {
if ($link['selected']) {
$link['title'] = Markup::create('<strong>' . $link['title'] . '</strong>');
}
$form['context_selection_' . $group_id][$link['value']] = [
'#type' => 'link',
'#url' => $link['href'],
'#title' => $link['title'],
'#prefix' => $pos > 0 ? ' | ' : '',
];
}
}
if (!$this->entity
->get('context')
->isEmpty()) {
$options = [];
foreach ($list as $id => $item) {
if ($config_pages
->id() != $id) {
$value = $item
->get('context')
->first()
->getValue();
$params = unserialize($value['value']);
$string = '';
if (is_array($params)) {
foreach ($params as $param) {
foreach ($param as $name => $val) {
$string .= $name . ' - ' . $val . ';';
}
}
$options[$id] = $string;
}
}
}
if (!empty($options)) {
$form['other_context'] = [
'#type' => 'details',
'#tree' => TRUE,
'#title' => t('Import'),
'#weight' => 1000,
];
$form['other_context']['list'] = [
'#type' => 'select',
'#options' => $options,
];
$form['other_context']['submit'] = [
'#type' => 'submit',
'#value' => t('Import'),
'#prefix' => '<div class="import-form-actions">',
'#suffix' => '</div>',
'#submit' => [
'::configPagesImportValues',
],
];
}
}
return $form;
}
public function configPagesClearValues(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$form_state
->setRedirectUrl(Url::fromRoute('entity.config_pages.clear_confirmation', [
'config_pages' => $entity
->id(),
]));
}
public function configPagesImportValues(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
if ($imported_entity_id = $form_state
->getValue('other_context')['list']) {
$entityStorage = $this->entityTypeManager
->getStorage('config_pages');
$imported_entity = $entityStorage
->load($imported_entity_id);
foreach ($entity as $name => &$value) {
if ($value
->getFieldDefinition() instanceof FieldConfigInterface) {
$entity
->set($name, $imported_entity
->get($name)
->getValue());
}
}
$entity
->save();
}
}
public function save(array $form, FormStateInterface $form_state) {
$config_pages = $this->entity;
$type = ConfigPagesType::load($config_pages
->bundle());
if (!$config_pages
->label()) {
$config_pages
->setLabel($type
->label());
}
$config_pages->context = $type
->getContextData();
if (!$form_state
->isValueEmpty('revision')) {
$config_pages
->setNewRevision();
}
$insert = $config_pages
->isNew();
$config_pages
->save();
$context = [
'@type' => $config_pages
->bundle(),
'%info' => $config_pages
->label(),
];
$logger = $this
->logger('config_pages');
$config_pages_type = $this->configPagesTypeStorage
->load($config_pages
->bundle());
$t_args = [
'@type' => $config_pages_type
->label(),
'%info' => $config_pages
->label(),
];
if ($insert) {
$logger
->notice('@type: added %info.', $context);
$this->messenger
->addStatus($this
->t('@type %info has been created.', $t_args));
}
else {
$logger
->notice('@type: updated %info.', $context);
$this->messenger
->addStatus($this
->t('@type %info has been updated.', $t_args));
}
if ($config_pages
->id()) {
$form_state
->setValue('id', $config_pages
->id());
$form_state
->set('id', $config_pages
->id());
}
else {
$this->messenger
->addError($this
->t('The config page could not be saved.'));
$form_state
->setRebuild();
}
}
protected function actions(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#submit' => [
'::submitForm',
'::save',
],
];
if (!$entity
->isNew()) {
$actions['reset'] = [
'#type' => 'submit',
'#value' => t('Clear values'),
'#submit' => [
'::configPagesClearValues',
],
'#button_type' => "submit",
'#access' => $this->user
->hasPermission('administer config_pages entity'),
];
}
return $actions;
}
}