Same name and namespace in other branches
  1. 8.x-1.x entity_redirect.module \entity_redirect_form_alter() 1 comment

Implements hook_form_alter().

File

./entity_redirect.module, line 54

Code

function entity_redirect_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    
    /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
    $form_object = $form_state->getFormObject();
    if (!is_a($form_object, '\\Drupal\\Core\\Entity\\EntityFormInterface')) {
        return;
    }
    $entity = $form_object->getEntity();
    if (is_a($entity, '\\Drupal\\Core\\Config\\Entity\\ConfigEntityBundleBase')) {
        if (is_a($form_object, '\\Drupal\\Core\\Entity\\EntityDeleteForm')) {
            return;
        }
        
        /** @var \Drupal\Core\Config\Entity\ConfigEntityBundleBase $entity */
        $settings = $entity->getThirdPartySettings('entity_redirect');
        $user = \Drupal::currentUser();
        $form['workflow']['entity_redirect'] = [
            '#type' => 'fieldset',
            '#title' => t('Redirect after Entity Operations'),
            '#tree' => TRUE,
        ];
        $actions = [
            'add' => t('Add'),
            'edit' => t('Edit'),
            'delete' => t('Delete'),
            'anonymous' => t('Override for Anonymous Users'),
        ];
        $destination = [
            'default' => t('- Default -'),
            'add_form' => t('Add Form'),
            'edit_form' => t('Return to Edit Form'),
            'url' => t('Local Url'),
            'created' => t('Created %entity_label', [
                '%entity_label' => $entity->label(),
            ]),
            'previous_page' => t('Return to the previous page'),
        ];
        $no_destionation_delete = [
            'edit_form' => t('Return to Edit Form'),
            'created' => t('Created %entity_label', [
                '%entity_label' => $entity->label(),
            ]),
        ];
        if (\Drupal::moduleHandler()->moduleExists('layout_builder')) {
            $destination['layout_builder'] = t('Go to Layout Builder Page');
        }
        foreach ($actions as $action => $title) {
            $options = $settings['redirect'][$action] ?? [];
            $form['workflow']['entity_redirect'][$action] = [
                '#type' => 'details',
                '#open' => FALSE,
                '#title' => $title,
                'active' => [
                    '#type' => 'checkbox',
                    '#title' => t('Enable'),
                    '#default_value' => $options['active'] ?? FALSE,
                ],
                'destination' => [
                    '#type' => 'select',
                    '#title' => t('Redirect Destination'),
                    '#options' => $action == 'delete' ? array_diff($destination, $no_destionation_delete) : $destination,
                    '#default_value' => $options['destination'] ?? 'default',
                    '#states' => [
                        'visible' => [
                            "[name='entity_redirect[{$action}][active]']" => [
                                'checked' => TRUE,
                            ],
                        ],
                    ],
                ],
                'url' => [
                    '#type' => 'path',
                    '#title' => t('Local Destination Url'),
                    '#description' => t('Path to redirect the user to after submission of forms for this entity. For example, type "/about" to redirect to that page. Use a relative path with a slash in front.'),
                    '#default_value' => $options['url'] ?? '',
                    '#convert_path' => PathElement::CONVERT_NONE,
                    '#states' => [
                        'visible' => [
                            "[name='entity_redirect[{$action}][destination]']" => [
                                'value' => 'url',
                            ],
                            "[name='entity_redirect[{$action}][active]']" => [
                                'checked' => TRUE,
                            ],
                        ],
                    ],
                ],
                'external' => [
                    '#type' => 'url',
                    '#title' => t('External Destination Url'),
                    '#description' => t('Enter a fully qualified url such as https://example.com/page.'),
                    '#default_value' => $options['external'] ?? '',
                    '#access' => $user->hasPermission('set external entity redirects'),
                    '#states' => [
                        'visible' => [
                            "[name='entity_redirect[{$action}][destination]']" => [
                                'value' => 'external',
                            ],
                            "[name='entity_redirect[{$action}][active]']" => [
                                'checked' => TRUE,
                            ],
                        ],
                    ],
                ],
            ];
            if ($user->hasPermission('set external entity redirects')) {
                $form['workflow']['entity_redirect'][$action]['destination']['#options']['external'] = t('External URL');
            }
        }
        $form['#entity_builders'][] = 'entity_redirect_bundle_builder';
    }
    elseif (is_a($entity, '\\Drupal\\Core\\Entity\\ContentEntityBase')) {
        
        /** @var \Drupal\Core\Entity\ContentEntityBase $entity */
        if (!($bundle_type = $entity->getEntityType()
            ->getBundleEntityType())) {
            return;
        }
        
        /** @var \Drupal\Core\Config\Entity\ConfigEntityBundleBase $bundle */
        $bundle = \Drupal::entityTypeManager()->getStorage($bundle_type)
            ->load($entity->bundle());
        if (!$bundle->getThirdPartySetting('entity_redirect', 'redirect')) {
            return;
        }
        // Create a hidden field to store the HTTP_REFERER variable.
        $form['http_referer'] = [
            '#type' => 'hidden',
            '#default_value' => $_SERVER['HTTP_REFERER'] ?? "",
        ];
        $form['actions']['submit']['#submit'][] = $entity->isNew() ? 'entity_redirect_new' : 'entity_redirect_submit';
    }
}