You are here

function block_attributes_form_block_form_alter in Block Attributes 8

Implements hook_form_FORM_ID_alter().

File

./block_attributes.module, line 10

Code

function block_attributes_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $block = $form_state
    ->getFormObject()
    ->getEntity()
    ->getPlugin();
  $config = $block
    ->getConfiguration();
  $attributes = \Drupal::config('block_attributes.config')
    ->get('attributes') ?: [];
  $form['settings']['attributes'] = [
    '#type' => 'details',
    '#title' => t('Attributes'),
    '#tree' => TRUE,
  ];
  $config_path = Url::fromRoute('block_attributes.config')
    ->toString();
  $referrer_path = parse_url(\Drupal::request()->headers
    ->get('referer'))['path'];
  $coming_from_config = $config_path == $referrer_path;

  // Open <details> element if coming from config page.
  if ($coming_from_config) {
    $form['settings']['attributes']['#open'] = TRUE;
  }
  $destination = \Drupal::destination()
    ->getAsArray();
  $config_path = Url::fromRoute('block_attributes.config', [], [
    'query' => $destination,
  ])
    ->toString();
  if (count($attributes)) {
    $form['settings']['attributes']['#description'] = '<small>' . t('Manage available attributes <a href="@config">here</a>.', [
      '@config' => $config_path,
    ]) . '</small>';
  }
  else {
    $form['settings']['attributes']['help'] = [
      '#markup' => t('Manage available attributes <a href="@config">here</a>.', [
        '@config' => $config_path,
      ]),
    ];
  }
  $autofocus = FALSE;

  // Iterate all defined attributes and create text field for them.
  foreach ($attributes as $attribute => $info) {

    // Provide default label / description for attributes.
    if (!$info['label']) {
      $info['label'] = str_replace('-', ' ', Unicode::ucfirst($attribute));
    }
    if (!$info['description']) {
      $info['description'] = t('Enter value for <code>@attribute</code> attribute.', [
        '@attribute' => $attribute,
      ]);
    }

    // Determine type based on options field.
    $type = !empty($info['options']) ? 'select' : 'textfield';
    $form['settings']['attributes'][$attribute] = [
      '#type' => $type,
      '#title' => $info['label'],
      '#description' => $info['description'],
      '#default_value' => isset($config['attributes'][$attribute]) ? $config['attributes'][$attribute] : '',
    ];

    // Fill options if select list.
    if ($type == 'select') {
      $form['settings']['attributes'][$attribute]['#empty_option'] = '- ' . t('Select value') . ' -';
      $form['settings']['attributes'][$attribute]['#options'] = $info['options'];
    }

    // Add "autofocus" attribute for first attribute input field
    // if coming from config page.
    if ($coming_from_config && !$autofocus) {
      $form['settings']['attributes'][$attribute]['#attributes'] = [
        'autofocus' => 'autofocus',
      ];
      $autofocus = TRUE;
    }
  }
  $form['actions']['submit']['#submit'][] = 'block_attributes_form_block_form_submit';
}