You are here

function block_form_alter_form_alter in Block Form Alter 8

Implements hook_form_alter().

File

./block_form_alter.module, line 13
This module provides block form alter functions.

Code

function block_form_alter_form_alter(&$form, FormStateInterface &$form_state, $form_id) {

  /*
   * Blocks are plugins; however, plugin information is not consistently
   * available from the various modules that render block forms (both core
   * and contrib). Resultantly, it is necessary to perform a variety of checks
   * to determine the plugin of a given block.
   *
   * Block forms for all plugins, excluding block_content and inline_block, can
   * be altered with hook_block_plugin_form_alter().
   *
   * Block forms for the block_content and inline_block plugins can be altered
   * with hook_block_type_form_alter().
   */

  // If block form is rendered by Block module.
  if ($form_id == 'block_form') {
    $build_info = $form_state
      ->getBuildInfo();
    $plugin_id = $build_info['callback_object']
      ->getEntity()
      ->getPluginId();
    _block_form_alter_block_plugin_form_alter_invoke($form, $form_state, $plugin_id);
  }
  elseif (preg_match('/block_content_(.*)_edit_form/', $form_id, $matches) || preg_match('/block_content_(.*)_form/', $form_id, $matches)) {
    $block_type = $matches[1];
    _block_form_alter_block_type_form_alter_invoke($form, $form_state, $block_type);
  }
  elseif ($form_id == 'layout_builder_add_block' || $form_id == 'layout_builder_update_block') {
    if ($form_id == 'layout_builder_add_block') {
      $storage = $form_state
        ->getStorage();
      $component_config = $storage['layout_builder__component']
        ->get('configuration');
    }
    elseif ($form_id == 'layout_builder_update_block') {
      $build_info = $form_state
        ->getBuildInfo();
      $block = $build_info['callback_object']
        ->getCurrentComponent();
      $component_config = $block
        ->get('configuration');
    }
    $block_id = explode(':', $component_config['id']);
    $plugin_id = $block_id[0];

    // Inline blocks.
    if ($plugin_id == 'inline_block') {

      // Because the block form is added with a process function for
      // inline blocks, it is necessary to alter them via a subsequent process
      // function.
      $form['settings']['block_form']['#process'][] = '_block_form_alter_block_type_form_alter_inline_block_process';
    }
    else {
      _block_form_alter_block_plugin_form_alter_invoke($form, $form_state, $plugin_id);
    }
  }
}