function ds_switch_view_mode_form_node_form_alter in Display Suite 8.3
Same name and namespace in other branches
- 8.4 modules/ds_switch_view_mode/ds_switch_view_mode.module \ds_switch_view_mode_form_node_form_alter()
- 8.2 modules/ds_switch_view_mode/ds_switch_view_mode.module \ds_switch_view_mode_form_node_form_alter()
Implements hook_form_BASE_FORM_ID_alter() for node_form.
Adds the switch view mode form element.
File
- modules/
ds_switch_view_mode/ ds_switch_view_mode.module, line 51 - Display Suite Switch View mode.
Code
function ds_switch_view_mode_form_node_form_alter(&$form, FormStateInterface $form_state) {
// Switch full view mode.
/* @var Drupal\node\NodeInterface $node */
$node = $form_state
->getFormObject()
->getEntity();
if (\Drupal::currentUser()
->hasPermission('ds switch ' . $node
->bundle()) || \Drupal::currentUser()
->hasPermission('ds switch view mode')) {
// Get the view modes.
$ds_vm = \Drupal::service('entity_display.repository')
->getViewModes('node');
$layouts = [];
$options = [
'' => t('Default'),
];
foreach ($ds_vm as $key => $item) {
$overridden = FALSE;
$entity_display = EntityViewDisplay::load('node.' . $node
->bundle() . '.' . $key);
if ($entity_display) {
$overridden = $entity_display
->status();
}
if ($overridden) {
$layout = Ds::getDisplay('node', $node
->bundle(), $key, FALSE);
$layouts[$key] = $layout;
$options[$key] = $item['label'];
}
}
// Add default layout settings.
$layouts[''] = Ds::getDisplay('node', $node
->bundle(), 'default', FALSE);
// Only fire if we have more than 1 option.
if (count($options) > 1) {
if (!isset($form['ds_switch_view_mode'])) {
$form['ds_switch_view_mode'] = [
'#type' => 'details',
'#title' => t('Display settings'),
'#weight' => 100,
'#group' => 'advanced',
];
}
$form['ds_switch_view_mode']['ds_switch'] = [
'#type' => 'select',
'#title' => t('View mode'),
'#options' => $options,
'#default_value' => $node->ds_switch->value,
'#description' => t('Switch to a different view mode to display the full page view of this node.'),
'#weight' => -1,
'#ajax' => [
'callback' => 'ds_switch_view_mode_switch_view_mode_preview_callback',
'wrapper' => 'ds_switch_preview_wrapper',
],
];
$form['ds_switch_view_mode']['preview'] = [
'#type' => 'container',
'#prefix' => '<div id="ds_switch_preview_wrapper">',
'#suffix' => '</div>',
];
$mode = $form_state
->getValue('ds_switch');
if (!$mode) {
$mode = $node
->get('ds_switch')->value;
}
$chosen_layout = $layouts[$mode];
if (empty($chosen_layout)) {
if (isset($layouts['full'])) {
$chosen_layout = $layouts['full'];
}
else {
$chosen_layout = $layouts[''];
}
}
$layout_settings = $chosen_layout
->getThirdPartySettings('ds');
$ds_layouts = Ds::getLayouts();
$fallback_image = drupal_get_path('module', 'ds') . '/images/preview.png';
if (isset($layout_settings['layout'])) {
/** @var \Drupal\Core\Layout\LayoutDefinition $chosen_layout */
$chosen_layout = $ds_layouts[$layout_settings['layout']['id']];
$image = $chosen_layout
->getIconPath();
if (empty($image)) {
$image = $fallback_image;
}
}
else {
$image = $fallback_image;
}
$form['ds_switch_view_mode']['preview']['image'] = [
'#markup' => '<div class="ds-layout-preview-image"><img src="' . base_path() . $image . '"/></div>',
];
}
}
}