You are here

public static function EntityconnectFormUtils::childFormAlter in Entity connect 8.2

Alters child create form.

We add a value field to hold the parent build_cache_id then we add a cancel button that run entityconnect_child_form_cancel and a new submit button.

Parameters

array $form: The child form.

\Drupal\Core\Form\FormStateInterface $form_state: Child form state.

string $form_id: Child form id.

string $cache_id: Cache id of parent data.

1 call to EntityconnectFormUtils::childFormAlter()
entityconnect_form_alter in ./entityconnect.module
Implements hook_form_alter().

File

src/EntityconnectFormUtils.php, line 173

Class

EntityconnectFormUtils
Contains form alter, callbacks and utility methods for entityconnect.

Namespace

Drupal\entityconnect

Code

public static function childFormAlter(array &$form, FormStateInterface $form_state, $form_id, $cache_id) {

  // Exclude some forms to be processed.
  $exclude_forms = [
    'search_block_form',
  ];

  // Allow other modules to alter exclude forms list.
  \Drupal::moduleHandler()
    ->alter('entityconnect_exclude_forms', $exclude_forms);
  if (in_array($form_id, $exclude_forms)) {
    return;
  }
  $form['parent_build_cache_id'] = [
    '#type' => 'value',
    '#value' => $cache_id,
  ];
  $form['actions']['cancel'] = [
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#submit' => [
      [
        '\\Drupal\\entityconnect\\EntityconnectFormUtils',
        'childFormCancel',
      ],
    ],
    '#parent_build_cache_id' => $cache_id,
    '#limit_validation_errors' => [],
    '#weight' => 1000,
  ];
  if (isset($form['submit']['#submit'])) {
    $form['submit']['#submit'][] = [
      '\\Drupal\\entityconnect\\EntityconnectFormUtils',
      'childFormSubmit',
    ];
  }
  else {
    foreach (array_keys($form['actions']) as $action) {
      if (!in_array($action, [
        'preview',
        'delete',
      ]) && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = [
          '\\Drupal\\entityconnect\\EntityconnectFormUtils',
          'childFormSubmit',
        ];
      }
    }
  }

  // Setup the child form delete button.
  if (!empty($form['actions']['delete']) && !empty($form['actions']['delete']['#type']) && strpos($form_id, '_confirm_delete') === FALSE && strpos($form_id, 'delete_form') === FALSE) {
    $delete_button =& $form['actions']['delete'];
    if ($delete_button['#type'] == 'link') {

      /** @var \Drupal\Core\Url $url */
      $url =& $delete_button['#url'];
      $url
        ->setOption('query', [
        'build_cache_id' => $cache_id,
        'child' => 1,
      ]);
    }
    elseif ($delete_button['#type'] == 'submit') {
      $form['actions']['delete']['#submit'][] = [
        '\\Drupal\\entityconnect\\EntityconnectFormUtils',
        'childFormDeleteSubmit',
      ];
    }
  }
  $data = [
    'form' => &$form,
    'form_state' => &$form_state,
    'form_id' => $form_id,
  ];
  \Drupal::moduleHandler()
    ->alter('entityconnect_child_form', $data);
}