You are here

public function BlockContent::blockTitleAjaxCallback in Quick Tabs 8.3

Ajax callback to change block title when block is selected.

File

src/Plugin/TabType/BlockContent.php, line 116

Class

BlockContent
Provides a 'block content' tab type.

Namespace

Drupal\quicktabs\Plugin\TabType

Code

public function blockTitleAjaxCallback(array &$form, FormStateInterface $form_state) {
  $tab_index = $form_state
    ->getTriggeringElement()['#array_parents'][2];
  $element_id = '#block-title-textfield-' . $tab_index;
  $selected_block = $form_state
    ->getValue('configuration_data')[$tab_index]['content']['block_content']['options']['bid'];
  $block_manager = \Drupal::service('plugin.manager.block');
  $context_repository = \Drupal::service('context.repository');
  $definitions = $block_manager
    ->getDefinitionsForContexts($context_repository
    ->getAvailableContexts());
  $form['block_title'] = [
    '#type' => 'textfield',
    '#value' => $definitions[$selected_block]['admin_label'],
    '#title' => $this
      ->t('Block Title'),
    '#prefix' => '<div id="block-title-textfield-' . $tab_index . '">',
    '#suffix' => '</div>',
  ];
  $form_state
    ->setRebuild(TRUE);
  $ajax_response = new AjaxResponse();
  $ajax_response
    ->addCommand(new ReplaceCommand($element_id, $form['block_title']));
  return $ajax_response;
}