You are here

function ajax_form_entity_form_alter in Ajax form entity 8

Same name and namespace in other branches
  1. 7 ajax_form_entity.module \ajax_form_entity_form_alter()
  2. 7.x ajax_form_entity.module \ajax_form_entity_form_alter()

Alter forms to ajaxify specified forms.

Implements hook_form_alter().

File

./ajax_form_entity.module, line 49
Contain ajax form entity module.

Code

function ajax_form_entity_form_alter(array &$form, FormStateInterface $form_state, $form_id) {

  // Work only on entity forms.
  $build_info = $form_state
    ->getBuildInfo();
  if (empty($build_info['callback_object'])) {
    return;
  }

  /* @var $callback Drupal\Core\Entity\EntityForm */
  $callback = $build_info['callback_object'];
  if (method_exists($callback, 'getEntity')) {

    // Get the entity.
    $entity = $callback
      ->getEntity();
    $entity_type = $entity
      ->getEntityTypeId();
    $bundle = $entity
      ->bundle();
    $entity_id = $entity
      ->id();

    // Get Ajax form entity configuration.
    $config_ajax_form_entity = \Drupal::config('ajax_form_entity.settings');
    $confs = $config_ajax_form_entity
      ->get('content');
    if (isset($confs[$entity_type][$bundle]['activate']) && $confs[$entity_type][$bundle]['activate']) {

      // Add form class for ajaxification. In case of add form, append "new"
      // instead of the entity ID.
      if ($entity_id) {
        $ajax_id = 'ajax-form-entity-' . $entity_type . '-' . $bundle . '-' . $entity_id;
      }
      else {
        $ajax_id = 'ajax-form-entity-' . $entity_type . '-' . $bundle . '-new';
      }
      $form['#attributes']['class'][] = $ajax_id;

      // Ajaxification settings of the buttons.
      $ajax_settings = [
        'callback' => 'Drupal\\ajax_form_entity\\Form\\FormAlter::ajaxFormEntityCallback',
        'wrapper' => $ajax_id,
        'effect' => 'fade',
      ];
      $form['actions']['submit']['#ajax'] = $ajax_settings;
      $form['actions']['publish']['#ajax'] = $ajax_settings;
      $form['actions']['unpublish']['#ajax'] = $ajax_settings;

      // @todo : does not work with dropbuttons (needs javascript magic presumably).
      unset($form['actions']['publish']['#dropbutton']);
      unset($form['actions']['unpublish']['#dropbutton']);

      // Ajaxify the buttons.
      foreach (array_keys($form['actions']) as $action) {
        if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
          $form['actions'][$action]['#submit'][] = 'Drupal\\ajax_form_entity\\Form\\FormAlter::ajaxFormEntityFormSubmit';
        }
      }

      // Handle case of entity edition : define the options.
      if ($entity_id) {
        $current_path = \Drupal::service('path.current')
          ->getPath();
        $path_args = explode('/', $current_path);

        // Case of edit link.
        if ($path_args[1] == 'ajax-form-entity') {
          $view_mode = $path_args[5];

          // Never reload the form.
          $reload = FALSE;

          // Always send content back for replacement.
          $send_content = TRUE;
          $selector_type = TRUE;
        }
        else {

          // @todo  : define display mode on creation.
          $view_mode = 'default';

          //$view_mode = $confs[$entity_type][$bundle]['view_mode'];

          // Always reload the form.
          $reload = 'reload_entity';
          $send_content = FALSE;
          $selector_type = FALSE;
        }

        // Selector is the class of content if popin or clas of form creation.
        if ($confs[$entity_type][$bundle]['popin'] || $path_args[1] != 'ajax-form-entity') {
          $content_selector = '.ajax-form-entity-view-' . $entity_type . '-' . $entity_id;
        }
        else {
          $content_selector = '.' . $ajax_id;
        }
      }
      else {

        // @todo  : define display mode on creation.
        $view_mode = 'default';

        //$view_mode = $confs[$entity_type][$bundle]['view_mode'];
        $reload = $confs[$entity_type][$bundle]['reload'];
        $send_content = $confs[$entity_type][$bundle]['send_content'];
        $selector_type = $confs[$entity_type][$bundle]['selector_type'];
        if ($confs[$entity_type][$bundle]['selector_content']) {
          $content_selector = $confs[$entity_type][$bundle]['selector_content'];
        }
        else {
          $content_selector = '.' . $ajax_id;
        }
      }

      // Add all configurations to the form to make it available everywhere.
      $form['ajax_form_entity'] = [
        '#type' => 'hidden',
        '#value' => [
          'view_mode' => $view_mode,
          'reload' => $reload,
          'popin' => $confs[$entity_type][$bundle]['popin'],
          'selector_type' => $selector_type,
          'send_content' => $send_content,
          'content_selector' => $content_selector,
          'form_selector' => '.' . $ajax_id,
          'show_message' => $confs[$entity_type][$bundle]['show_message'],
        ],
      ];

      // @todo : handle preview and deletion.
    }
  }
}