You are here

function gutenberg_form_node_form_alter in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 gutenberg.module \gutenberg_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.

Hides mapping fields.

File

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

Code

function gutenberg_form_node_form_alter(&$form, FormStateInterface $form_state) {
  $node = $form_state
    ->getFormObject()
    ->getEntity();
  if (!_gutenberg_is_gutenberg_enabled($node)) {

    // Leave early if Gutenberg is not enabled for this entity.
    return;
  }
  $config = \Drupal::service('config.factory')
    ->get('gutenberg.settings');
  $node_type = $node->type
    ->getString();

  // Set template options to global var.
  $gutenberg_template = json_decode($config
    ->get($node_type . '_template'));
  $form['#attached']['drupalSettings']['gutenberg']['template'] = $gutenberg_template;
  $form['#attached']['drupalSettings']['gutenberg']['template-lock'] = $config
    ->get($node_type . '_template_lock');

  /* @var $mapping_fields \Drupal\gutenberg\MappingFieldsHelper */
  $mapping_fields = \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(MappingFieldsHelper::class);
  $mapped_fields = $mapping_fields
    ->getMappedFields($gutenberg_template);
  foreach ($mapped_fields as $item) {
    $form[$item['field']]['#access'] = FALSE;
  }

  /** @var \Drupal\gutenberg\GutenbergLibraryManagerInterface $gutenberg_library_manager */
  $gutenberg_library_manager = \Drupal::service('plugin.manager.gutenberg.library');
  $module_definitions = $gutenberg_library_manager
    ->getModuleDefinitions();
  foreach ($module_definitions as $module_definition) {
    foreach ($module_definition['libraries-edit'] as $library) {
      $form['#attached']['library'][] = $library;
    }
  }
  $theme_support = [];
  $theme_definitions = $gutenberg_library_manager
    ->getActiveThemeDefinitions();
  foreach ($theme_definitions as $theme_definition) {
    foreach ($theme_definition['libraries-edit'] as $library) {
      $form['#attached']['library'][] = $library;
    }
    if (isset($theme_definition['theme-support'])) {

      // Merge only the top level configuration, with the child theme
      // taking precedence.
      $theme_support = $theme_definition['theme-support'] + $theme_support;
    }
  }
  $form['#attached']['drupalSettings']['gutenberg']['theme-support'] = $theme_support;

  // Set available image sizes for editor.
  $styles = ImageStyle::loadMultiple();
  $sizes = [
    [
      'slug' => 'full',
      'name' => t('Original'),
    ],
  ];
  foreach ($styles as $style) {
    $sizes[] = [
      'slug' => $style
        ->getName(),
      'name' => $style
        ->label(),
    ];
  }
  $form['#attached']['drupalSettings']['gutenberg']['image-sizes'] = $sizes;
  $form['#attached']['drupalSettings']['gutenberg']['is-rtl'] = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getDirection() === LanguageInterface::DIRECTION_RTL;
  $text_fields = UtilsController::getEntityTextFields($node);
  $form['#attributes']['class'][] = 'metabox-base-form';
  $form[$text_fields[0]]['widget'][0]['#format'] = 'gutenberg';
  $form[$text_fields[0]]['#attributes']['class'][] = 'field--gutenberg';

  // Hide the field label.
  $form[$text_fields[0]]['widget'][0]['#title_display'] = 'hidden';

  // Disable the summary field.
  if (isset($form[$text_fields[0]]['widget'][0]['summary'])) {
    $form[$text_fields[0]]['widget'][0]['summary']['#access'] = FALSE;
  }
  foreach ($text_fields as $fieldname) {

    // For the rest of the text fields call after build to remove
    // Gutenberg from text format options.
    if ($text_fields[0] !== $fieldname) {
      $form[$fieldname]['widget']['#after_build'][] = 'gutenberg_form_node_form_after_build';
    }
  }

  // Let's move the remaining fields to a "special"
  // form group that can be used later by JS to move to
  // Gutenberg's sidebar.
  $form['metabox_fields'] = [
    '#type' => 'details',
    '#access' => TRUE,
    '#title' => t('More settings'),
    '#weight' => 99,
    // Group fallback in case JS fails to move to metaboxes.
    '#group' => 'advanced',
  ];

  // Some other module might have already init this container.
  if (!isset($form['additional_fields'])) {
    $form['additional_fields'] = [
      '#type' => 'container',
      '#title' => 'Additional',
      '#weight' => -100,
    ];
  }

  // Move title to Published/meta pane.
  $form['title']['#group'] = 'meta';

  // Move status to Published/meta pane.
  $form['status']['#group'] = 'meta';

  // Move langcode to Published/meta pane.
  if (isset($form['langcode'])) {
    $form['langcode']['#group'] = 'meta';
  }
  $excluded_fields = [
    'status',
    'title',
    'uid',
    'created',
    'changed',
    'promote',
    'sticky',
    'path',
    'comment',
    'revision_log',
    'langcode',
  ];

  /*
   * Rationale behind this "messy" algo:
   * If there's any details fieldset on the form, add it to a special array
   * and then, on the form after build, add its #id to a JS array.
   * For any other type of fields, group them on the metabox_fields fieldset.
   * This fieldset will also move to metaboxes area.
   */
  $metabox_has_fields = FALSE;
  $fields_with_details = [];
  $field_names = UtilsController::getEntityFieldNames($node);
  foreach ($field_names as $value) {
    if (array_key_exists($value, $form) && $value !== $text_fields[0] && !in_array($value, $excluded_fields)) {
      if (isset($form[$value]['widget']) && isset($form[$value]['widget'][0]) && isset($form[$value]['widget'][0]['#type']) && $form[$value]['widget'][0]['#type'] === 'details') {
        $fields_with_details[] = $value;
      }
      else {
        $form[$value]['#group'] = 'metabox_fields';
        $metabox_has_fields = TRUE;
      }
    }
  }
  $form['#after_build'][] = 'gutenberg_form_node_form_details_after_build';
  $form['#fields_with_details'] = $fields_with_details;
  if (!$metabox_has_fields) {
    unset($form['metabox_fields']);
  }

  // Is Bartik the default theme? Add some custom styles
  // to look even better.
  $default_theme = \Drupal::config('system.theme')
    ->get('default');
  if ($default_theme === 'bartik') {
    $form['#attached']['library'][] = 'gutenberg/bartik';
  }
  $theme = \Drupal::theme()
    ->getActiveTheme();
  $theme_name = $theme
    ->getName();

  // Check if current theme is Seven (admin)
  if ($theme_name === 'seven') {
    $form['#attached']['library'][] = 'gutenberg/seven';
  }

  // Check if current theme is Claro (admin)
  if ($theme_name === 'claro') {
    $form['#attached']['library'][] = 'gutenberg/claro';
  }
  $form['#attached']['drupalSettings']['gutenberg']['metaboxes'][] = 'edit-metabox-fields';

  /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
  $module_handler = \Drupal::service('module_handler');
  $form['#attached']['drupalSettings']['gutenberg']['media-enabled'] = $module_handler
    ->moduleExists('media');
  if ($form['#attached']['drupalSettings']['gutenberg']['media-library-enabled'] = $module_handler
    ->moduleExists('media_library')) {
    $form['#attached']['library'][] = 'media_library/ui';
  }
}