You are here

function layout_builder_blocks_form_alter in Layout Builder Blocks 1.0.x

Implements hook_form_alter().

File

./layout_builder_blocks.module, line 13
Layout Builder Blocks module file.

Code

function layout_builder_blocks_form_alter(&$form, FormStateInterface $form_state) {
  if ($form['#form_id'] === 'layout_builder_add_block' || $form['#form_id'] === 'layout_builder_update_block') {
    $allowed = FALSE;

    /** @var \Drupal\layout_builder\Form\ConfigureBlockFormBase $form_object */
    $form_object = $form_state
      ->getFormObject();
    $component = $form_object
      ->getCurrentComponent();
    $block_plugin_id = $component
      ->getPluginId();
    $config = \Drupal::config('layout_builder_blocks.styles');

    // If this is a reusable block, retrieve the block bundle.
    $bundle = FALSE;
    if (strpos($block_plugin_id, "block_content:") === 0) {
      $uuid = str_replace('block_content:', '', $block_plugin_id);
      $bundle = \Drupal::service('entity.repository')
        ->loadEntityByUuid('block_content', $uuid)
        ->bundle();
    }
    if (empty($config
      ->get('block_restrictions')) || in_array($block_plugin_id, $config
      ->get('block_restrictions')) || $bundle && in_array('inline_block:' . $bundle, $config
      ->get('block_restrictions'))) {

      // Return parent form.
      $allowed = TRUE;
    }
    if ($allowed) {

      // Our main set of tabs.
      $form['ui'] = [
        '#type' => 'container',
        '#weight' => -100,
        '#attributes' => [
          'id' => 'bs_ui',
        ],
        '#tree' => FALSE,
        '#parents' => [
          'ui',
        ],
      ];
      $tabs = [
        [
          'machine_name' => 'content',
          'icon' => 'content.svg',
          'title' => t('Content'),
          'active' => TRUE,
        ],
        [
          'machine_name' => 'appearance',
          'icon' => 'appearance.svg',
          'title' => t('Style'),
        ],
      ];

      // Create our tabs from above.
      $form['ui']['nav_tabs'] = [
        '#type' => 'html_tag',
        '#tag' => 'ul',
        '#attributes' => [
          'class' => 'bs_nav-tabs',
          'id' => 'bs_nav-tabs',
          'role' => 'tablist',
        ],
      ];
      $form['ui']['tab_content'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => 'blb_tab-content',
          'id' => 'bs_tabContent',
        ],
        '#tree' => FALSE,
        '#parents' => [
          'tab_content',
        ],
      ];

      // Create our tab & tab panes.
      foreach ($tabs as $tab) {
        $form['ui']['nav_tabs'][$tab['machine_name']] = [
          '#type' => 'inline_template',
          '#template' => '<li><a data-target="{{ target|clean_class }}" class="{{active}}"><span role="img">{% include icon %}</span>{{ title }}</a></li>',
          '#context' => [
            'title' => $tab['title'],
            'target' => $tab['machine_name'],
            'active' => isset($tab['active']) && $tab['active'] == TRUE ? 'active' : '',
            'icon' => drupal_get_path('module', 'bootstrap_styles') . '/images/ui/' . ($tab['icon'] ? $tab['icon'] : 'default.svg'),
          ],
        ];
        $form['ui']['tab_content'][$tab['machine_name']] = [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'bs_tab-pane',
              'bs_tab-pane--' . $tab['machine_name'],
              isset($tab['active']) && $tab['active'] == TRUE ? 'active' : '',
            ],
          ],
          '#tree' => FALSE,
          '#parents' => [
            $tab['machine_name'],
          ],
        ];
      }
      $form['ui']['tab_content']['appearance']['#tree'] = TRUE;
      $configuration = [
        'bootstrap_styles' => [
          'block_style' => [],
        ],
      ];
      if ($component
        ->get('bootstrap_styles')) {
        $bootstrap_styles = $component
          ->get('bootstrap_styles');
        if (isset($bootstrap_styles['block_style'])) {
          $configuration['bootstrap_styles'] = $bootstrap_styles;
        }
      }
      $form['ui']['tab_content']['appearance'] += \Drupal::service('plugin.manager.bootstrap_styles_group')
        ->buildStylesFormElements($form['ui']['tab_content']['appearance'], $form_state, $configuration['bootstrap_styles']['block_style'], 'layout_builder_blocks.styles');

      // Attach Bootstrap Styles base library.
      $form['#attached']['library'][] = 'bootstrap_styles/layout_builder_form_style';
      $form['#attached']['library'][] = 'layout_builder_blocks/content_tab';
      array_unshift($form['#submit'], '_layout_builder_blocks_submit_block_form');
    }
  }
}