You are here

function block_titlelink_form_alter in Block Title Link 8

Same name and namespace in other branches
  1. 6.2 block_titlelink.module \block_titlelink_form_alter()
  2. 6 block_titlelink.module \block_titlelink_form_alter()
  3. 7 block_titlelink.module \block_titlelink_form_alter()

Implementation of hook_form_alter

File

./block_titlelink.module, line 15
module for adding a link to a block title

Code

function block_titlelink_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'block_form' || $form_id == 'basic_custom_block_form') {
    $target_values = [
      '_blank',
      '_self',
      '_parent',
      '_top',
    ];
    $targets = array_combine($target_values, $target_values);
    $config = \Drupal::config('block_titlelink.settings');
    $block_id = $form['id']['#default_value'];
    $display = $config
      ->get($block_id . '.display_link');
    $display = isset($display) ? $display : TRUE;
    $form['block_titlelink'] = [
      '#type' => 'details',
      '#title' => t('Block Title Link Settings'),
      '#collapsible' => TRUE,
      '#weight' => 0,
      '#open' => FALSE,
    ];
    $form['block_titlelink']['title_link'] = [
      '#type' => 'textfield',
      '#title' => t('Title Path'),
      '#description' => t('URL path of Block Title. Tokens are supported.'),
      '#default_value' => $config
        ->get($block_id . '.title_link'),
    ];
    $form['block_titlelink']['title_link_title'] = [
      '#type' => 'textfield',
      '#title' => t('Title Attribute'),
      '#description' => t('Value for the <a> title attribute.'),
      '#default_value' => $config
        ->get($block_id . '.title'),
    ];
    $form['block_titlelink']['title_link_target'] = [
      '#type' => 'select',
      '#title' => t('Link Target'),
      '#options' => $targets,
      '#empty_value' => '',
      '#description' => t('Add a target to open the link in a new tab/window'),
      '#default_value' => $config
        ->get($block_id . '.target'),
    ];
    $form['block_titlelink']['display_link'] = [
      '#type' => 'checkbox',
      '#title' => t('Display Link'),
      '#description' => t('Select this option if title should render as a link. If deselected, the title path value is stored within the block object as $block->title_link but is not rendered. This is useful if you wish to use the link elsewhere in the block template (ex: as an icon).'),
      '#default_value' => $display,
    ];
    $form['actions']['submit']['#validate'][] = 'block_titlelink_form_validate';
    $form['actions']['submit']['#submit'][] = 'block_titlelink_form_submit';
  }
}