class ChangeLayoutForm in Display Suite 8.2
Same name and namespace in other branches
- 8.4 src/Form/ChangeLayoutForm.php \Drupal\ds\Form\ChangeLayoutForm
- 8.3 src/Form/ChangeLayoutForm.php \Drupal\ds\Form\ChangeLayoutForm
Provides a configuration form for configurable actions.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\ds\Form\ChangeLayoutForm
Expanded class hierarchy of ChangeLayoutForm
1 string reference to 'ChangeLayoutForm'
File
- src/
Form/ ChangeLayoutForm.php, line 12
Namespace
Drupal\ds\FormView source
class ChangeLayoutForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'ds_change_layout';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $entity_type = '', $bundle = '', $display_mode = '', $new_layout = '') {
$old_layout = NULL;
$all_layouts = Ds::getLayouts();
if (!empty($entity_type) && !empty($bundle) && !empty($display_mode)) {
$display = entity_get_display($entity_type, $bundle, $display_mode);
$old_layout = $display
->getThirdPartySettings('ds');
}
if ($old_layout && isset($all_layouts[$new_layout])) {
$new_layout_key = $new_layout;
$new_layout = $all_layouts[$new_layout];
$old_layout_info = $all_layouts[$old_layout['layout']['id']];
$form['#entity_type'] = $entity_type;
$form['#entity_bundle'] = $bundle;
$form['#mode'] = $display_mode;
$form['#old_layout'] = $old_layout;
$form['#old_layout_info'] = $old_layout_info;
$form['#new_layout'] = $new_layout;
$form['#new_layout_key'] = $new_layout_key;
$form['info'] = array(
'#markup' => $this
->t('You are changing from @old to @new layout for @bundle in @view_mode view mode.', array(
'@old' => $old_layout_info['label'],
'@new' => $new_layout['label'],
'@bundle' => $bundle,
'@view_mode' => $display_mode,
)),
'#prefix' => "<div class='change-ds-layout-info'>",
'#suffix' => "</div>",
);
// Old region options.
$regions = array();
foreach ($old_layout_info['regions'] as $key => $info) {
$regions[$key] = $info['label'];
}
// Let other modules alter the regions.
// For old regions.
$context = array(
'entity_type' => $entity_type,
'bundle' => $bundle,
'view_mode' => $display_mode,
);
$region_info = array(
'region_options' => $regions,
);
\Drupal::moduleHandler()
->alter('ds_layout_region', $context, $region_info);
$regions = $region_info['region_options'];
$form['#old_layout_info']['layout']['regions'] = $regions;
// For new regions.
$new_regions = array();
foreach ($new_layout['regions'] as $key => $info) {
$new_regions[$key] = $info['label'];
}
$region_info = array(
'region_options' => $new_regions,
);
\Drupal::moduleHandler()
->alter('ds_layout_region', $context, $region_info);
$new_layout['regions'] = $region_info['region_options'];
$form['#new_layout']['regions'] = $new_layout['regions'];
// Display the region options.
$selectable_regions = array(
'' => $this
->t('- None -'),
) + $new_layout['regions'];
$form['regions_pre']['#markup'] = '<div class="ds-layout-regions">';
foreach ($regions as $region => $region_title) {
$form['region_' . $region] = array(
'#type' => 'container',
);
$form['region_' . $region]['ds_label_' . $region] = array(
'#markup' => 'Fields in <span class="change-ds-layout-old-region"> ' . $region_title . '</span> go into',
);
$form['region_' . $region]['ds_' . $region] = array(
'#type' => 'select',
'#options' => $layout_options = $selectable_regions,
'#default_value' => $region,
);
}
$form['regions_post']['#markup'] = '</div>';
// Show previews from old and new layouts.
$form['preview'] = array(
'#type' => 'container',
'#prefix' => '<div class="ds-layout-preview">',
'#suffix' => '</div>',
);
$fallback_image = drupal_get_path('module', 'ds') . '/images/preview.png';
$old_image = isset($old_layout_info['icon']) && !empty($old_layout_info['icon']) ? $old_layout_info['icon'] : $fallback_image;
$new_image = isset($new_layout['icon']) && !empty($new_layout['icon']) ? $new_layout['icon'] : $fallback_image;
$arrow = drupal_get_path('module', 'ds') . '/images/arrow.png';
$form['preview']['old_layout'] = array(
'#markup' => '<div class="ds-layout-preview-image"><img src="' . base_path() . $old_image . '"/></div>',
);
$form['preview']['arrow'] = array(
'#markup' => '<div class="ds-layout-preview-arrow"><img src="' . base_path() . $arrow . '"/></div>',
);
$form['preview']['new_layout'] = array(
'#markup' => '<div class="ds-layout-preview-image"><img src="' . base_path() . $new_image . '"/></div>',
);
$form['#attached']['library'][] = 'ds/admin';
// Submit button.
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#prefix' => '<div class="ds-layout-change-save">',
'#suffix' => '</div>',
);
}
else {
$form['nothing'] = array(
'#markup' => $this
->t('No valid configuration found.'),
);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Prepare some variables.
$old_layout = $form['#old_layout'];
$new_layout = $form['#new_layout'];
$old_layout_info = $form['#old_layout_info'];
$new_layout_key = $form['#new_layout_key'];
$entity_type = $form['#entity_type'];
$bundle = $form['#entity_bundle'];
$display_mode = $form['#mode'];
// Create new third party settings.
$third_party_settings = $old_layout;
$third_party_settings['layout']['id'] = $new_layout_key;
if (!empty($new_layout['library'])) {
$third_party_settings['layout']['library'] = $new_layout['library'];
}
$third_party_settings['layout']['path'] = $new_layout['path'];
unset($third_party_settings['regions']);
// Map old regions to new ones.
foreach ($old_layout_info['layout']['regions'] as $region => $region_title) {
$new_region = $form_state
->getValue('ds_' . $region);
if ($new_region != '' && isset($old_layout['regions'][$region])) {
foreach ($old_layout['regions'][$region] as $field) {
if (!isset($third_party_settings['regions'][$new_region])) {
$third_party_settings['regions'][$new_region] = array();
}
$third_party_settings['regions'][$new_region][] = $field;
}
}
}
// Save configuration.
/* @var $entity_display EntityDisplayInterface*/
$entity_display = entity_load('entity_view_display', $entity_type . '.' . $bundle . '.' . $display_mode);
foreach (array_keys($third_party_settings) as $key) {
$entity_display
->setThirdPartySetting('ds', $key, $third_party_settings[$key]);
}
$entity_display
->save();
// Clear entity info cache.
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
// Show message.
drupal_set_message(t('The layout change has been saved.'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ChangeLayoutForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ChangeLayoutForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ChangeLayoutForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
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 | |
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:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
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. | |
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. |