class ConfigPagesForm in Config Pages 8
Same name and namespace in other branches
- 8.3 src/ConfigPagesForm.php \Drupal\config_pages\ConfigPagesForm
- 8.2 src/ConfigPagesForm.php \Drupal\config_pages\ConfigPagesForm
Form controller for the custom config page edit forms.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\config_pages\ConfigPagesForm
- class \Drupal\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of ConfigPagesForm
File
- src/
ConfigPagesForm.php, line 20
Namespace
Drupal\config_pagesView source
class ConfigPagesForm extends ContentEntityForm {
/**
* The custom config page storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $ConfigPagesStorage;
/**
* The custom config page type storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $ConfigPagesTypeStorage;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The config page content entity.
*
* @var \Drupal\config_pages\ConfigPagesInterface
*/
protected $entity;
/**
* Constructs a ConfigPagesForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager.
* @param \Drupal\Core\Entity\EntityStorageInterface $config_pages_storage
* The custom config page storage.
* @param \Drupal\Core\Entity\EntityStorageInterface $config_pages_type_storage
* The custom config page type storage.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
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;
// @to_do: this should be removed when parent class will be refactored and will not use entityManager anymore.
$this->entityManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* Overrides \Drupal\Core\Entity\EntityForm::prepareEntity().
*
* Prepares the custom config page object.
*
* Fills in a few default values, and then invokes
* hook_config_pages_prepare() on all modules.
*/
protected function prepareEntity() {
$config_pages = $this->entity;
// Set up default values, if required.
$config_pages_type = $this->ConfigPagesTypeStorage
->load($config_pages
->bundle());
}
/**
* {@inheritdoc}
*/
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 context message.
$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(),
));
}
// Override the default CSS class name, since the user-defined custom config page
// type name in 'TYPE-config-page-form' potentially clashes with third-party class
// names.
$form['#attributes']['class'][0] = 'config-page-' . Html::getClass($config_pages
->bundle()) . '-form';
// Add context import fieldset if any CP exists at this moment.
if (!$this->entity
->get('context')
->isEmpty()) {
$options = [];
foreach ($list as $id => $item) {
// Build options list.
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;
}
}
}
// Show form if any data available.
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;
}
/**
* Form submit.
*
* Clear field values submit callback.
*/
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(),
)));
}
/**
* Form submit.
*
* Import other context submit callback.
*/
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) {
// Process only fields added from BO.
if ($value
->getFieldDefinition() instanceof FieldConfigInterface) {
$entity
->set($name, $imported_entity
->get($name)
->getValue());
}
}
$entity
->save();
}
}
/**
* {@inheritdoc}
*/
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();
// Save as a new revision if requested to do so.
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 {
// In the unlikely case something went wrong on save, the config page will be
// rebuilt and config page form redisplayed.
drupal_set_message($this
->t('The config page could not be saved.'), 'error');
$form_state
->setRebuild();
}
}
/**
* Returns an array of supported actions for the current entity form.
*
* @todo Consider introducing a 'preview' action here, since it is used by
* many entity types.
*/
protected function actions(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
// Save ConfigPage entity.
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#submit' => array(
'::submitForm',
'::save',
),
];
if (!$entity
->isNew()) {
// Add button to reset values.
$actions['reset'] = [
'#type' => 'submit',
'#value' => t('Clear values'),
'#submit' => array(
'::configPagesClearValues',
),
'#button_type' => "submit",
];
}
return $actions;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigPagesForm:: |
protected | property | The custom config page storage. | |
ConfigPagesForm:: |
protected | property | The custom config page type storage. | |
ConfigPagesForm:: |
protected | property |
The config page content entity. Overrides ContentEntityForm:: |
|
ConfigPagesForm:: |
protected | property | The language manager. | |
ConfigPagesForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
ConfigPagesForm:: |
public | function | Form submit. | |
ConfigPagesForm:: |
public | function | Form submit. | |
ConfigPagesForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ContentEntityForm:: |
|
ConfigPagesForm:: |
public | function |
Gets the actual form array to be built. Overrides ContentEntityForm:: |
|
ConfigPagesForm:: |
protected | function |
Overrides \Drupal\Core\Entity\EntityForm::prepareEntity(). Overrides ContentEntityForm:: |
|
ConfigPagesForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
ConfigPagesForm:: |
public | function |
Constructs a ConfigPagesForm object. Overrides ContentEntityForm:: |
|
ContentEntityForm:: |
protected | property | The entity repository service. | |
ContentEntityForm:: |
protected | property | The entity type bundle info service. | |
ContentEntityForm:: |
protected | property | The time service. | |
ContentEntityForm:: |
protected | function | Add revision form fields if the entity enabled the UI. | |
ContentEntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityForm:: |
3 |
ContentEntityForm:: |
protected | function |
Copies top-level form values to entity properties Overrides EntityForm:: |
|
ContentEntityForm:: |
protected | function | Flags violations for the current form. | 4 |
ContentEntityForm:: |
protected | function | Returns the bundle entity of the entity, or NULL if there is none. | |
ContentEntityForm:: |
protected | function | Gets the names of all fields edited in the form. | 4 |
ContentEntityForm:: |
public | function |
Gets the form display. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
public | function |
Gets the code identifying the active form language. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function | Should new revisions created on default. | |
ContentEntityForm:: |
protected | function |
Initializes the form state and the entity before the first form build. Overrides EntityForm:: |
1 |
ContentEntityForm:: |
protected | function | Initializes form language code values. | |
ContentEntityForm:: |
public | function |
Checks whether the current form language matches the entity one. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
public | function |
Sets the form display. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function | Checks whether the revision form fields should be added to the form. | |
ContentEntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
3 |
ContentEntityForm:: |
public | function | Updates the changed time of the entity. | |
ContentEntityForm:: |
public | function | Updates the form language to reflect any change to the entity language. | |
ContentEntityForm:: |
public | function |
Button-level validation handlers are highly discouraged for entity forms,
as they will prevent entity validation from running. If the entity is going
to be saved during the form submission, this method should be manually
invoked from the button-level… Overrides FormBase:: |
3 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |