You are here

function gutenberg_form_alter in Gutenberg 8

Same name and namespace in other branches
  1. 8.2 gutenberg.module \gutenberg_form_alter()

Implements hook_form_alter().

File

./gutenberg.module, line 23
Provides integration with the Gutenberg editor.

Code

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

  // TODO: There's got to be a better way to alter all media forms regardless
  // the type.
  if ($form_id == 'media_video_edit_form' || $form_id == 'media_image_edit_form' || $form_id == 'media_audio_edit_form' || $form_id == 'media_file_edit_form' || $form_id == 'media_remote_video_edit_form') {
    _gutenberg_media_form_alter($form, $form_state, $form_id);
  }
  if ($form_id == 'node_type_edit_form' || $form_id == 'node_type_add_form') {
    $config = \Drupal::service('config.factory')
      ->getEditable('gutenberg.settings');
    $form['gutenberg'] = [
      '#type' => 'details',
      '#title' => t('Gutenberg experience'),
      '#description' => '',
      '#group' => 'additional_settings',
      '#weight' => 999,
      'enable_gutenberg_experience' => [
        '#type' => 'checkbox',
        '#title' => t('Enable Gutenberg experience'),
        '#description' => t('Turn the node edit form into a full Gutenberg UI experience. At least one field of long text type is necessary.'),
        '#default_value' => $config
          ->get($form['type']['#default_value'] . '_enable_full'),
      ],
    ];
    $form['gutenberg']['categories'] = [
      '#type' => 'hidden',
      '#default_value' => [],
    ];
    $example_code = "<br/><code>[<br/>&nbsp;&nbsp;[\"core/heading\", {}],<br/>&nbsp;&nbsp;[\"core/paragraph\", {\"placeholder\": \"Insert text\"}]<br/>]</code>";
    $form['gutenberg']['gutenberg_template'] = [
      '#type' => 'textarea',
      '#description' => t('JSON structure of blocks. Example:') . $example_code,
      '#title' => t('Template'),
      '#default_value' => $config
        ->get($form['type']['#default_value'] . '_template'),
      '#states' => [
        'visible' => [
          'input[name="enable_gutenberg_experience"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['gutenberg']['gutenberg_template_lock'] = [
      '#type' => 'select',
      '#title' => t('Template lock'),
      '#description' => t('<code>All</code> will fully lock the page template, not able to delete, create or move blocks just edit the content.<br/><code>Insert</code> will allow moving blocks.'),
      '#default_value' => $config
        ->get($form['type']['#default_value'] . '_template_lock'),
      '#options' => [
        'none' => t('None'),
        'insert' => t('Insert'),
        'all' => t('All'),
      ],
      '#states' => [
        'visible' => [
          'input[name="enable_gutenberg_experience"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['gutenberg']['allowed_blocks_details'] = [
      '#type' => 'details',
      '#title' => t('Allowed Gutenberg blocks'),
      '#states' => [
        'visible' => [
          'input[name="enable_gutenberg_experience"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $settings = _gutenberg_get_allowed_blocks();
    $blocks_settings = UtilsController::getBlocksSettings();
    foreach ($settings['categories'] as $category) {
      $category['reference'] = str_replace('/', '-', $category['reference']);
      $form['gutenberg']['categories']['#default_value'][] = $category['reference'];
      $form['gutenberg']['allowed_blocks_details'][$category['reference']] = [
        '#type' => 'fieldset',
        '#title' => $category['name'],
      ];
      $options = [
        $category['reference'] . '/all' => t('All'),
      ];
      foreach ($category['blocks'] as $block) {
        if (!in_array($block['id'], $blocks_settings['blacklist'])) {
          $options[$block['id']] = $block['name'];
        }
      }
      $default_values = array_combine(array_map(function ($block) {
        return $block['id'];
      }, $category['blocks']), array_map(function ($block) {
        return $block['default'] ? $block['id'] : 0;
      }, $category['blocks']));
      $config_values = $config
        ->get($form['type']['#default_value'] . '_allowed_blocks');
      $form['gutenberg']['allowed_blocks_details'][$category['reference']]['allowed_blocks_' . $category['reference']] = [
        '#type' => 'checkboxes',
        '#options' => $options,
        '#default_value' => array_merge($default_values, $config_values ? $config_values : []),
      ];
    }
    $form['gutenberg']['allowed_drupal_blocks_details'] = [
      '#type' => 'details',
      '#title' => t('Allowed Drupal blocks'),
      '#states' => [
        'visible' => [
          'input[name="enable_gutenberg_experience"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $blockManager = \Drupal::service('plugin.manager.block');
    $contextRepository = \Drupal::service('context.repository');

    // Get blocks definition
    // $definitions = $blockManager->getDefinitionsForContexts($contextRepository->getAvailableContexts());
    $definitions = $blockManager
      ->getFilteredDefinitions('block_ui', $contextRepository
      ->getAvailableContexts());
    $groups = $blockManager
      ->getGroupedDefinitions($definitions);
    $form['gutenberg']['categories_drupal'] = [
      '#type' => 'hidden',
      '#default_value' => [],
    ];
    $default_values = array_fill_keys(array_map(function ($key) {
      return str_replace('drupalblock/', '', $key);
    }, $settings['default_drupal_blocks']), TRUE);
    foreach ($groups as $key => $blocks) {
      $group_reference = preg_replace('@[^a-z0-9-]+@', '_', strtolower($key));
      $options = [];
      $input_default_values = [];
      foreach ($blocks as $key_block => $block) {
        if (!in_array('drupalblock/' . $key_block, $blocks_settings['blacklist'])) {
          $options[$key_block] = $block['admin_label'];
          if (isset($default_values[$key_block]) && $default_values[$key_block]) {
            $input_default_values[$key_block] = $key_block;
          }
        }
      }
      if (count($options) > 0) {
        $form['gutenberg']['categories_drupal']['#default_value'][] = $group_reference;
        $form['gutenberg']['allowed_drupal_blocks_details'][$group_reference] = [
          '#type' => 'fieldset',
          '#title' => $key,
        ];
        $config_values = $config
          ->get($form['type']['#default_value'] . '_allowed_drupal_blocks');
        $form['gutenberg']['allowed_drupal_blocks_details'][$group_reference]['allowed_drupal_blocks_' . $group_reference] = [
          '#type' => 'checkboxes',
          '#options' => $options,
          '#default_value' => array_merge($input_default_values, $config_values ? $config_values : []),
        ];
      }
    }
    $form['#attached']['library'][] = 'gutenberg/admin';
    $form['actions']['submit']['#submit'][] = '_gutenberg_node_type_form_submit';
    if (isset($form['actions']['save_continue']['#submit'])) {
      $form['actions']['save_continue']['#submit'][] = '_gutenberg_node_type_form_submit';
    }
  }
}