You are here

function ds_extras_form_entity_view_display_edit_form_alter in Display Suite 8.3

Same name and namespace in other branches
  1. 8.2 modules/ds_extras/ds_extras.module \ds_extras_form_entity_view_display_edit_form_alter()

Implements hook_form_FORM_ID_alter().

File

modules/ds_extras/ds_extras.module, line 58
Display Suite extras main functions.

Code

function ds_extras_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state) {

  // Region to block only fires if there is a layout and we're working on the
  // a view mode which is not equal to default.
  $mode = $form_state
    ->getFormObject()
    ->getEntity()
    ->getMode();
  if (isset($form['#ds_layout']) && $mode != 'default' && \Drupal::config('ds_extras.settings')
    ->get('region_to_block')) {

    // Get the entity_type, bundle and view mode.
    $entity_type = $form['#entity_type'];
    $bundle = $form['#bundle'];
    $view_mode = $mode;
    $region_blocks_options = [];
    $region_blocks = \Drupal::config('ds_extras.settings')
      ->get('region_blocks');
    if (!empty($region_blocks)) {
      foreach ($region_blocks as $key => $block) {
        if ($block['info'] == "{$entity_type}_{$bundle}_{$view_mode}") {
          $region_blocks_options[$key] = t('Remove') . ' ' . $block['title'];
        }
      }
    }
    $form['region_to_block'] = [
      '#type' => 'details',
      '#group' => 'additional_settings',
      '#title' => t('Block regions (deprecated)'),
      '#description' => t('Create additional regions in this layout which will be exposed as blocks. Will be removed in Drupal 9 version.'),
    ];
    $url = Url::fromUri('https://www.drupal.org/node/2754967');
    $link = Link::fromTextAndUrl(t('handbook page'), $url)
      ->toString();
    $form['region_to_block']['deprecated'] = [
      '#markup' => '<b>' . t('This functionality is not compatible with some caching modules. Read the @handbook for a better alternative.', [
        '@handbook' => $link,
      ]) . '</b>',
    ];
    $form['region_to_block']['new_block_region'] = [
      '#type' => 'textfield',
      '#title' => t('Region name'),
      '#description' => t('Enter a name to create a new region.'),
    ];
    $form['region_to_block']['new_block_region_key'] = [
      '#title' => t('Machine name'),
      '#type' => 'machine_name',
      '#default_value' => '',
      '#maxlength' => 32,
      '#required' => FALSE,
      '#description' => t('The machine-readable name of this block region. This name must contain only lowercase letters and underscores. This name must be unique.'),
      '#disabled' => FALSE,
      '#machine_name' => [
        'exists' => 'ds_extras_region_to_block_unique',
        'source' => [
          'region_to_block',
          'new_block_region',
        ],
      ],
    ];
    if (!empty($region_blocks_options)) {
      $form['region_to_block']['remove_block_region'] = [
        '#type' => 'checkboxes',
        '#title' => t('Existing block regions'),
        '#options' => $region_blocks_options,
        '#description' => t('Check the regions you want to remove.'),
      ];
    }
    $form['actions']['submit']['#submit'][] = 'ds_extras_block_submit';
  }
}