You are here

function _ds_field_ui_table_layouts_preview in Display Suite 8.3

Same name and namespace in other branches
  1. 8.4 includes/field_ui.inc \_ds_field_ui_table_layouts_preview()
  2. 8.2 includes/field_ui.inc \_ds_field_ui_table_layouts_preview()
  3. 7.2 includes/ds.field_ui.inc \_ds_field_ui_table_layouts_preview()

Add the layout previews to the Field UI screen.

Parameters

array $form: A collection of form properties.

\Drupal\Core\Form\FormStateInterface $form_state: The state of the form.

array $ds_layouts: Collection of all the layouts.

array $layout: Current selected layout.

\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display: The entity display object.

1 call to _ds_field_ui_table_layouts_preview()
_ds_field_ui_table_layouts in includes/field_ui.inc
Add the layouts fieldset on the Field UI screen.

File

includes/field_ui.inc, line 934
Field UI functions for Display Suite.

Code

function _ds_field_ui_table_layouts_preview(array &$form, FormStateInterface $form_state, array $ds_layouts, array $layout, EntityViewDisplayInterface $display) {
  $layout_string = '';
  $form['ds_layouts']['ds_layout'] = [
    '#type' => 'select',
    '#title' => t('Select a layout'),
    '#options' => $layout['layout_options'],
    '#default_value' => isset($layout['layout']) ? $layout['layout'] : NULL,
    '#empty_value' => '_none',
    '#weight' => -1,
    '#ajax' => [
      'callback' => 'ds_field_ui_table_layouts_preview_callback',
      'wrapper' => 'ds_layout_wrapper',
    ],
  ];
  if (!isset($layout['layout'])) {
    $form['ds_layouts']['ds_layout']['#description'] = t("A layout must be selected to enable Display Suite functionality.");
  }
  $form['ds_layouts']['preview'] = [
    '#type' => 'container',
    '#prefix' => '<div id="ds_layout_wrapper">',
    '#suffix' => '</div>',
    '#weight' => -3,
  ];
  if (isset($layout['layout']) || $form_state
    ->hasValue('ds_layout')) {
    $layout_string = $form_state
      ->hasValue('ds_layout') ? $form_state
      ->getValue('ds_layout') : $layout['layout'];
  }
  $suggestions_array = [];
  if (!empty($layout_string)) {
    if (isset($ds_layouts[$layout_string])) {

      /** @var \Drupal\Core\Layout\LayoutDefinition $chosen_layout */
      $chosen_layout = $ds_layouts[$layout_string];
      $selected = '<strong>' . $chosen_layout
        ->getLabel() . '</strong>';
      $selected .= '<br/>' . t('The default template can be found in %path', [
        '%path' => $chosen_layout
          ->getPath() . '/' . $chosen_layout
          ->getTemplate(),
      ]);
      $suggestions_array[0] = $chosen_layout
        ->getTemplate() . '--' . $display
        ->getTargetEntityTypeId();
      $suggestions_array[2] = $chosen_layout
        ->getTemplate() . '--' . $display
        ->getTargetEntityTypeId() . '-' . $display
        ->getTargetBundle();
      $suggestions_array[4] = $chosen_layout
        ->getTemplate() . '--' . $display
        ->getTargetEntityTypeId() . '--{id}';
      if ($display
        ->getMode() != 'default') {
        $suggestions_array[1] = $chosen_layout
          ->getTemplate() . '--' . $display
          ->getTargetEntityTypeId() . '-' . $display
          ->getMode();
        $suggestions_array[3] = $chosen_layout
          ->getTemplate() . '--' . $display
          ->getTargetEntityTypeId() . '-' . $display
          ->getTargetBundle() . '-' . $display
          ->getMode();
      }
    }
    ksort($suggestions_array);

    // Append correct extension.
    foreach ($suggestions_array as $key => $value) {
      $suggestion_replace = strtr($suggestions_array[$key], '_', '-');
      $suggestions_array[$key] = $suggestion_replace . '.html.twig';
    }
    if ($form_state
      ->hasValue('ds_layout') || !empty($layout) && isset($layout['regions']) && !empty($chosen_layout)) {
      $fallback_image = drupal_get_path('module', 'ds') . '/images/preview.png';
      $current_layout = $form_state
        ->hasValue('ds_layout') && (!isset($layout->layout) || $form_state
        ->getValue('ds_layout') != $layout->layout) ? t('Current layout (after save)') : t('Current layout');
      $image = $chosen_layout
        ->getIconPath();
      if (empty($image)) {
        $image = $fallback_image;
      }
      $form['ds_layouts']['preview']['title'] = [
        '#markup' => '<div class="ds-layout-preview-title">' . $current_layout . '</div>',
      ];
      $form['ds_layouts']['preview']['image'] = [
        '#markup' => '<div class="ds-layout-preview-image"><img src="' . base_path() . $image . '"/></div>',
      ];
      $form['ds_layouts']['preview']['info'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'ds-layout-preview-suggestion',
          ],
        ],
      ];
      $form['ds_layouts']['preview']['info']['suggestions'] = [
        '#markup' => '<p>' . $selected . '</p><p>' . t('Template suggestions') . ':' . '</p>',
      ];
      $form['ds_layouts']['preview']['info']['suggestions_list'] = [
        '#theme' => 'item_list',
        '#items' => $suggestions_array,
      ];
      if (!empty($chosen_layout
        ->getLibrary())) {
        $disable_css = FALSE;
        if (isset($layout['disable_css'])) {
          $disable_css = $layout['disable_css'];
        }
        if ($form_state
          ->hasValue('disable_css') && $disable_css !== $form_state
          ->getValue('disable_css')) {
          $disable_css = $form_state
            ->getValue('disable_css');
        }
        $form['ds_layouts']['preview']['info']['settings']['disable_css'] = array(
          '#type' => 'checkbox',
          '#title' => t('Disable layout CSS styles'),
          '#default_value' => $disable_css,
        );
      }
      $entity_classes = 'all_classes';
      if (isset($layout['entity_classes'])) {
        $entity_classes = $layout['entity_classes'];
      }
      if ($form_state
        ->hasValue('entity_classes') && $entity_classes !== $form_state
        ->getValue('entity_classes')) {
        $entity_classes = $form_state
          ->getValue('entity_classes');
      }

      // Default classes.
      $form['ds_layouts']['preview']['info']['settings']['entity_classes'] = [
        '#type' => 'select',
        '#title' => t('Entity classes'),
        '#options' => [
          'all_classes' => t('Entity, bundle and view mode'),
          'no_classes' => t('No classes'),
          'old_view_mode' => t('View mode (deprecated)'),
        ],
        '#default_value' => $entity_classes,
      ];
      $form['ds_layouts']['preview']['clear'] = [
        '#markup' => '<div class="ds-after-suggestion"></div>',
      ];
    }
    if (!isset($layout['layout']) || $layout_string != $layout['layout']) {

      // Get admin path.
      $route = FieldUI::getOverviewRouteInfo($display
        ->getTargetEntityTypeId(), $display
        ->getTargetBundle());
      $route_parameters = $route
        ->getRouteParameters();
      $route_parameters['view_mode_name'] = $display
        ->getMode();
      $options = $route
        ->getOptions();
      $route_name = 'entity.entity_view_display.' . $display
        ->getTargetEntityTypeId() . '.view_mode';
      $admin_path = \Drupal::service('url_generator')
        ->generateFromRoute($route_name, $route_parameters, $options);
      $destination_url = '';

      // If regions aren't set we don't have to move fields.
      if (isset($layout['regions'])) {
        $route_name = 'ds.change_layout';
        $route_parameters = [
          'entity_type' => $display
            ->getTargetEntityTypeId(),
          'bundle' => $display
            ->getTargetBundle(),
          'display_mode' => $display
            ->getMode(),
          'new_layout' => $layout_string,
        ];
        $options = [];
        $destination_url = $admin_path;
      }
      $form['layout_changed_url'] = [
        '#type' => 'value',
        '#value' => [
          'route_name' => $route_name,
          'route_parameters' => $route_parameters,
          'destination_url' => $destination_url,
          'options' => $options,
        ],
      ];
      array_unshift($form['actions']['submit']['#submit'], 'ds_field_ui_change_layout_submit');
    }
  }
}