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\EntityConstraintViolationList;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\Core\Url;
class ConfigPagesForm extends ContentEntityForm {
protected $ConfigPagesStorage;
protected $ConfigPagesTypeStorage;
protected $languageManager;
protected $entity;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityStorageInterface $config_pages_storage, EntityStorageInterface $config_pages_type_storage, LanguageManagerInterface $language_manager) {
$this->ConfigPagesStorage = $config_pages_storage;
$this->ConfigPagesTypeStorage = $config_pages_type_storage;
$this->languageManager = $language_manager;
$this->entityTypeManager = $entity_type_manager;
$this->entityManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($entity_type_manager, $entity_type_manager
->getStorage('config_pages'), $entity_type_manager
->getStorage('config_pages_type'), $container
->get('language_manager'));
}
protected function prepareEntity() {
$config_pages = $this->entity;
$config_pages_type = $this->ConfigPagesTypeStorage
->load($config_pages
->bundle());
}
public function form(array $form, FormStateInterface $form_state) {
$config_pages = $this->entity;
$account = $this
->currentUser();
$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) {
drupal_set_message($this
->t('Please note that this Page is context sensitive, current context is %label', array(
'%label' => $label,
)), 'warning');
}
if ($this->operation == 'edit') {
$form['#title'] = $this
->t('Edit custom config page %label', array(
'%label' => $config_pages
->label(),
));
}
$form['#attributes']['class'][0] = 'config-page-' . Html::getClass($config_pages
->bundle()) . '-form';
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']);
$params = array_shift($params);
$string = '';
if (is_array($params)) {
foreach ($params as $name => $val) {
$string .= $name . ' - ' . $val . ';';
}
$options[$id] = $string;
}
}
}
if (!empty($options)) {
$form['other_context'] = [
'#type' => 'details',
'#tree' => TRUE,
'#title' => t('Import'),
];
$form['other_context']['list'] = [
'#type' => 'select',
'#options' => $options,
];
$form['other_context']['submit'] = [
'#type' => 'submit',
'#value' => t('Import'),
'#prefix' => '<div class="imort-form-actions">',
'#suffix' => '</div>',
'#submit' => array(
'::configPagesImportValues',
),
];
}
}
return $form;
}
public function configPagesClearValues(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$form_state
->setRedirectUrl(Url::fromRoute('entity.config_pages.clear_confirmation', array(
'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);
drupal_set_message($this
->t('@type %info has been created.', $t_args));
}
else {
$logger
->notice('@type: updated %info.', $context);
drupal_set_message($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 {
drupal_set_message($this
->t('The config page could not be saved.'), 'error');
$form_state
->setRebuild();
}
}
protected function actions(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#submit' => array(
'::submitForm',
'::save',
),
];
if (!$entity
->isNew()) {
$actions['reset'] = [
'#type' => 'submit',
'#value' => t('Clear values'),
'#submit' => array(
'::configPagesClearValues',
),
'#button_type' => "submit",
];
}
return $actions;
}
}