class SkinEditForm in Skinr 8.2
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\skinr_ui\Form\SkinEditForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of SkinEditForm
File
- skinr_ui/
src/ Form/ SkinEditForm.php, line 17 - Contains Drupal\skinr_ui\Form\SkinEditForm.
Namespace
Drupal\skinr_ui\FormView source
class SkinEditForm extends EntityForm {
/**
* The entity query factory.
*
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
protected $entityQueryFactory;
/**
* @param \Drupal\Core\Entity\Query\QueryFactory $entity_query_factory
* The entity query.
*/
public function __construct(QueryFactory $entity_query_factory) {
$this->entityQueryFactory = $entity_query_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.query'));
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
// Override save button label.
if ($this->entity
->isNew()) {
$actions['submit']['#value'] = $this
->t('Add');
}
return $actions;
}
/**
* Handles switching the available elements based on the selected theme and element type.
*/
public function updateElement($form, FormStateInterface $form_state) {
$theme_name = $form_state
->getValue('theme');
$element_type = $form_state
->getValue('element_type');
$form['element']['#options'] = self::elementOptions($theme_name, $element_type);
Select::processSelect($form['element'], $form_state, $form);
return $form['element'];
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/* @var Skin $skin */
$skin = $this->entity;
$theme_handler = \Drupal::service('theme_handler');
if ($skin
->isNew()) {
$themes = $theme_handler
->listInfo();
$form['theme'] = array(
'#type' => 'select',
'#title' => t('Theme'),
'#options' => array_map(function ($theme) {
return $theme->info['name'];
}, $themes),
'#default_value' => $form_state
->getValue('theme'),
'#required' => TRUE,
'#ajax' => array(
'callback' => '::updateElement',
'wrapper' => 'dropdown-element-replace',
),
);
$form['element_type'] = array(
'#type' => 'select',
'#title' => t('Type'),
'#options' => skinr_get_config_info(),
'#default_value' => $form_state
->getValue('element_type'),
'#required' => TRUE,
'#ajax' => array(
'callback' => '::updateElement',
'wrapper' => 'dropdown-element-replace',
),
);
$form['element'] = array(
'#type' => 'select',
'#title' => t('Element'),
'#prefix' => '<div id="dropdown-element-replace">',
'#suffix' => '</div>',
'#options' => self::elementOptions($form_state
->getValue('theme'), $form_state
->getValue('element_type')),
'#required' => TRUE,
);
$skin_infos = skinr_get_skin_info();
// Apply overridden status to skins.
foreach ($skin_infos as $skin_name => $skin_info) {
$skin_infos[$skin_name]['status'] = skinr_skin_info_status_get($skin_infos[$skin_name]);
}
// @todo Only display enabled skins.
// @todo Group by groups.
$form['skin'] = array(
'#type' => 'select',
'#title' => t('Skin'),
'#options' => array_map(function ($skin_info) {
return $skin_info['title'];
}, $skin_infos),
'#required' => TRUE,
);
}
else {
$form['info']['element_type_info'] = array(
'#type' => 'item',
'#title' => t('Type'),
'#markup' => $skin
->elementTypeLabel(),
);
$form['info']['element_info'] = array(
'#type' => 'item',
'#title' => t('Element'),
'#markup' => $skin
->elementLabel(),
);
$form['info']['theme_info'] = array(
'#type' => 'item',
'#title' => t('Theme'),
'#markup' => $skin
->themeLabel(),
);
$form['info']['skin_info'] = array(
'#type' => 'item',
'#title' => t('Skin'),
'#markup' => $skin
->skinLabel(),
);
$form['element_type'] = array(
'#type' => 'value',
'#value' => $skin->element_type,
);
$form['element'] = array(
'#type' => 'value',
'#value' => $skin->element,
);
$form['theme'] = array(
'#type' => 'value',
'#value' => $skin->theme,
);
$form['skin'] = array(
'#type' => 'value',
'#value' => $skin->skin,
);
$skin_infos = skinr_get_skin_info();
// Add custom info.
$skin_infos['_additional'] = array(
'title' => t('Additional'),
);
$skin_info = $skin_infos[$skin->skin];
// Create options widget.
$field = array();
if (!empty($skin_info['form callback'])) {
// Process custom form callbacks.
// Load include file.
if (!empty($skin_info['source']['include file'])) {
skinr_load_include($skin_info['source']['include file']);
}
// Execute form callback.
if (function_exists($skin_info['form callback'])) {
$context = array(
'theme' => $skin->theme,
'skin_name' => $skin->skin,
'skin_info' => $skin_info,
'value' => isset($defaults[$skin->theme][$skin->skin]) ? $defaults[$skin->theme][$skin->skin] : array(),
);
$field = $skin_info['form callback']($form, $form_state, $context);
}
}
elseif ($skin->skin == '_additional') {
$field = array(
'#type' => 'textfield',
'#title' => t('CSS classes'),
'#size' => 40,
'#description' => t('To add CSS classes manually, enter classes separated by a single space i.e. <code>first-class second-class</code>'),
'#default_value' => $skin
->getOptions(),
);
if (skinr_ui_access('edit advanced skin settings')) {
$field['#disabled'] = TRUE;
$field['#description'] .= '<br /><em>' . t('You require additional permissions to edit this setting.') . '</em>';
}
}
else {
switch ($skin_info['type']) {
case 'checkboxes':
$field = array(
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#title' => $skin_info['title'],
'#options' => $this
->optionsToFormOptions($skin_info['options']),
'#default_value' => $skin
->getOptions(),
'#description' => $skin_info['description'],
'#weight' => isset($skin_info['weight']) ? $skin_info['weight'] : NULL,
);
break;
case 'radios':
$field = array(
'#type' => 'radios',
'#title' => $skin_info['title'],
'#options' => array_merge(array(
'' => '<none>',
), $this
->optionsToFormOptions($skin_info['options'])),
'#default_value' => $skin
->getOptions(),
'#description' => $skin_info['description'],
'#weight' => isset($skin_info['weight']) ? $skin_info['weight'] : NULL,
);
break;
case 'select':
$field = array(
'#type' => 'select',
'#title' => $skin_info['title'],
'#options' => array_merge(array(
'' => '<none>',
), $this
->optionsToFormOptions($skin_info['options'])),
'#default_value' => $skin
->getOptions(),
'#description' => $skin_info['description'],
'#weight' => isset($skin_info['weight']) ? $skin_info['weight'] : NULL,
);
break;
default:
// Raise an error.
drupal_set_message(t("Widget %name's type is invalid.", array(
'%name' => $skin->skin,
)), 'error', FALSE);
break;
}
}
$form['options'] = $field;
}
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$status = parent::save($form, $form_state);
$destination = \Drupal::service('redirect.destination')
->getAsArray();
if ($destination['destination'] == base_path() . 'admin/structure/skinr/add') {
$form_state
->setRedirect('entity.skin.edit_form', array(
'skin' => $this->entity
->id(),
));
}
elseif ($destination['destination'] == base_path() . 'admin/structure/skinr/' . $this->entity
->id() . '/edit') {
$form_state
->setRedirect('skinr_ui.list');
}
return $status;
}
/**
* Helper function to convert an array of options, as specified in the .info
* file, into an array usable by Form API.
*
* @param $options
* An array containing at least the 'class' and 'label' keys.
*
* @return string[]
* A Form API compatible array of options.
*
* @todo Rename function to be more descriptive.
*/
protected function optionsToFormOptions($options) {
$form_options = array();
foreach ($options as $option_name => $option) {
$form_options[$option_name] = $option['title'];
}
return $form_options;
}
/**
* Return an array of element options for a module.
*
* If no field type is provided, returns a nested array of all element options,
* keyed by module.
*
* @param string $theme_name
* @param string $element_type
*
* @return array
*/
protected function elementOptions($theme_name = NULL, $element_type = NULL) {
$options =& drupal_static(__FUNCTION__);
if (!isset($options)) {
$options = skinr_invoke_all('skinr_ui_element_options', $theme_name);
}
if ($element_type && isset($options[$element_type])) {
if (!empty($theme_name)) {
$theme = \Drupal::service('theme_handler')
->getTheme($theme_name);
$theme_label = $theme->info['name'];
if (isset($options[$element_type][$theme_label])) {
return $options[$element_type][$theme_label];
}
}
return $options[$element_type];
}
return array();
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 being used by this form. | 7 |
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 |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
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 | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
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 |
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 FormInterface:: |
17 |
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. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
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. | |
SkinEditForm:: |
protected | property | The entity query factory. | |
SkinEditForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
SkinEditForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
SkinEditForm:: |
protected | function | Return an array of element options for a module. | |
SkinEditForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
SkinEditForm:: |
protected | function | Helper function to convert an array of options, as specified in the .info file, into an array usable by Form API. | |
SkinEditForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
SkinEditForm:: |
public | function | Handles switching the available elements based on the selected theme and element type. | |
SkinEditForm:: |
public | function | ||
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. |