Commit fd09bb40 authored by paul121's avatar paul121 Committed by Sascha Eggenberger
Browse files

Issue #3188521: Improve content form detection

parent a5779883
Loading
Loading
Loading
Loading

gin.api.php

0 → 100644
+58 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Hooks for gin theme.
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Register routes to apply Gin’s content edit form layout.
 *
 * Leverage this hook to achieve a consistent user interface layout on
 * administrative edit forms, similar to the node edit forms. Any module
 * providing a custom entity type or form mode may wish to implement this
 * hook for their form routes. Please note that not every content entity
 * form route should enable the Gin edit form layout, for example the
 * delete entity form does not need it.
 *
 * @return array
 *   An array of route names.
 *
 * @see GinContentFormHelper->isContentForm()
 * @see hook_gin_content_form_routes_alter()
 */
function hook_gin_content_form_routes() {
  return [
    // Layout a custom node form.
    'entity.node.my_custom_form',

    // Layout a custom entity type edit form.
    'entity.my_type.edit_form',
  ];
}

/**
 * Alter the registered routes to enable or disable Gin’s edit form layout.
 *
 * @param array $routes
 *   The list of routes.
 *
 * @return array
 *   An array of route names.
 *
 * @see GinContentFormHelper->isContentForm()
 * @see hook_gin_content_form_routes()
 */
function hook_gin_content_form_routes_alter(array &$routes) {
  // Example: disable Gin edit form layout customizations for an entity type.
  $routes = array_diff($routes, ['entity.my_type.edit_form']);
}

/**
 * @} End of "addtogroup hooks".
 */
+2 −74
Original line number Diff line number Diff line
@@ -6,86 +6,14 @@
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\gin\GinContentFormHelper;
use Drupal\gin\GinSettings;

/**
 * Implements form_alter_HOOK() for some major form changes.
 */
function gin_form_alter(&$form, $form_state, $form_id) {
  // Are we on an edit form?
  if (_gin_is_content_form($form, $form_state, $form_id)) {
    // Action buttons.
    if (isset($form['actions'])) {
      if (isset($form['actions']['preview'])) {
        // Put Save after Preview.
        $save_weight = $form['actions']['preview']['#weight'] ? $form['actions']['preview']['#weight'] + 1 : 11;
        $form['actions']['submit']['#weight'] = $save_weight;
      }

      // Move entity_save_and_addanother_node after preview.
      if (isset($form['actions']['entity_save_and_addanother_node'])) {
        // Put Save after Preview.
        $save_weight = $form['actions']['entity_save_and_addanother_node']['#weight'];
        $form['actions']['preview']['#weight'] = $save_weight - 1;
      }

      // Create gin_actions group.
      $form['gin_actions'] = [
        '#type' => 'container',
        '#weight' => -1,
        '#multilingual' => TRUE,
        '#attributes' => [
          'class' => [
            'gin-sticky',
          ],
        ],
      ];
      // Assign status to gin_actions.
      $form['status']['#group'] = 'gin_actions';

      // Create actions group.
      $form['gin_actions']['actions'] = [
        '#type' => 'actions',
        '#weight' => 130,
      ];

      // Move all actions over.
      $form['gin_actions']['actions'] = ($form['actions']) ?? [];
      // Now let's just remove delete, as we'll move that over to gin_sidebar.
      unset($form['gin_actions']['actions']['delete']);

      // Create gin_sidebar group.
      $form['gin_sidebar'] = [
        '#group' => 'meta',
        '#type' => 'container',
        '#weight' => 99,
        '#multilingual' => TRUE,
        '#attributes' => [
          'class' => [
            'gin-sidebar',
          ],
        ],
      ];
      // Copy footer over.
      $form['gin_sidebar']['footer'] = ($form['footer']) ?? [];
      // Copy delete action.
      $form['gin_sidebar']['actions'] = [];
      $form['gin_sidebar']['actions']['#type'] = ($form['actions']['#type']) ?? [];
      $form['gin_sidebar']['actions']['delete'] = ($form['actions']['delete']) ?? [];
    }

    // Attach library.
    $form['#attached']['library'][] = 'gin/edit_form';
  }

  // If not logged in hide changed and author node info on add forms.
  $not_logged_in = \Drupal::currentUser()->isAnonymous();
  $route = \Drupal::routeMatch()->getRouteName();

  if ($not_logged_in && $route == 'node.add') {
    unset($form['meta']['changed']);
    unset($form['meta']['author']);
  }
  \Drupal::classResolver(GinContentFormHelper::class)->formAlter($form, $form_state, $form_id);

  // User form (Login, Register or Forgot password).
  if (strpos($form_id, 'user_login') !== FALSE || strpos($form_id, 'user_register') !== FALSE || strpos($form_id, 'user_pass') !== FALSE) {
+0 −46
Original line number Diff line number Diff line
@@ -135,49 +135,3 @@ function _gin_validate_path_logo($path) {
  }
  return FALSE;
}

/**
 * Check if were on a content edit form.
 */
function _gin_is_content_form($form = NULL, $form_state = NULL, $form_id = NULL) {
  $is_content_form = FALSE;

  // Get route name.
  $route_name = \Drupal::routeMatch()->getRouteName();

  // Routes to include.
  $route_names = [
    'node.add',
    'entity.node.content_translation_add',
    'entity.node.content_translation_edit',
    'quick_node_clone.node.quick_clone',
    'entity.node.edit_form',
  ];

  if (
    in_array($route_name, $route_names, TRUE) ||
    ($form_state && ($form_state->getBuildInfo()['base_form_id'] ?? NULL) === 'node_form') ||
    ($route_name === 'entity.group_content.create_form' && strpos($form_id, 'group_node') === FALSE)
  ) {
    $is_content_form = TRUE;
  }

  // Forms to exclude.
  // If media library widget, don't use new content edit form.
  // gin_preprocess_html is not triggered here, so checking
  // the form id is enough.
  $form_ids_to_ignore = [
    'media_library_add_form_',
    'views_form_media_library_widget_',
    'views_exposed_form',
    'date_recur_modular_sierra_occurrences_modal',
  ];

  foreach ($form_ids_to_ignore as $form_id_to_ignore) {
    if ($form_id && strpos($form_id, $form_id_to_ignore) !== FALSE) {
      $is_content_form = FALSE;
    }
  }

  return $is_content_form;
}
+2 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
 * html.theme
 */

use Drupal\gin\GinContentFormHelper;
use Drupal\gin\GinSettings;

/**
@@ -32,7 +33,7 @@ function gin_preprocess_html(&$variables) {
    }

    // Edit form? Use the new Gin Edit form layout.
    if (_gin_is_content_form()) {
    if (\Drupal::classResolver(GinContentFormHelper::class)->isContentForm()) {
      $variables['attributes']['class'][] = 'gin--edit-form';
    }

+7 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
 * page.theme
 */

use Drupal\gin\GinContentFormHelper;
use Drupal\Core\Entity\EntityInterface;
use Drupal\gin\GinSettings;

@@ -46,6 +47,12 @@ function gin_theme_suggestions_page_alter(&$suggestions, $variables) {
    $arg = str_replace(["/", '-'], ['_', '_'], $path);
    $suggestions[] = 'page__' . $arg;
  }

  // The node page template is required to use the node content form.
  if (\Drupal::classResolver(GinContentFormHelper::class)->isContentForm()
    && !in_array('page__node', $suggestions)) {
    $suggestions[] = 'page__node';
  }
}

/**
Loading