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

Implements hook_form_alter().

File

./entity_redirect.module, line 17

Code

function entity_redirect_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    $entity_type_forms = [
        'node_type_edit_form',
        'media_bundle_edit_form',
        'taxonomy_vocabulary_form',
        'commerce_product_type_add_form',
        'commerce_product_type_edit_form',
    ];
    $entity_forms = [
        'node_form',
        'media_form',
        'taxonomy_term_form',
        'commerce_product_form',
    ];
    if (in_array($form_id, $entity_type_forms)) {
        $entity = $form_state->getFormObject()
            ->getEntity();
        $settings = $entity->getThirdPartySettings('entity_redirect');
        $form['workflow']['entiry_redirect'] = [
            '#type' => 'fieldset',
            '#title' => t('Redirect on Save'),
            'destination' => [
                '#type' => 'select',
                '#title' => t('Redirect Destination'),
                '#options' => [
                    'default' => t('- Default -'),
                    'add_form' => t('Add Form'),
                    'url' => t('Local Url'),
                    'created' => t('Created %entity_label', [
                        '%entity_label' => $entity->label(),
                    ]),
                ],
                '#default_value' => isset($settings['destination']) ? $settings['destination'] : 'default',
            ],
            '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' => isset($settings['url']) ? $settings['url'] : '',
                '#convert_path' => PathElement::CONVERT_NONE,
                '#states' => [
                    'visible' => [
                        '#edit-destination' => [
                            'value' => 'url',
                        ],
                    ],
                ],
            ],
            'external' => [
                '#type' => 'url',
                '#title' => t('External Destination Url'),
                '#description' => t('Enter a fully qualified url such as https://example.com/page.'),
                '#default_value' => isset($settings['external']) ? $settings['external'] : '',
                '#access' => \Drupal::currentUser()->hasPermission('set external entity redirects'),
                '#states' => [
                    'visible' => [
                        '#edit-destination' => [
                            'value' => 'external',
                        ],
                    ],
                ],
            ],
            'personalizable' => [
                '#type' => 'checkbox',
                '#title' => t('Allow Per User Settings'),
                '#default_value' => isset($settings['personalizable']) ? $settings['personalizable'] : TRUE,
                '#description' => t('Allow individual users to control their own redirect destination. If enabled and the user has permision they can change it on their profile edit pages.'),
            ],
            'redirect_edit' => [
                '#type' => 'checkbox',
                '#title' => t('Redirect after editing'),
                '#default_value' => isset($settings['redirect_edit']) ? $settings['redirect_edit'] : FALSE,
                '#description' => t('By default only redirects on creation of new entities, check to also apply when editing.'),
            ],
        ];
        if (\Drupal::currentUser()->hasPermission('set external entity redirects')) {
            $form['workflow']['entiry_redirect']['destination']['#options']['external'] = t('External URL');
        }
        $form['#entity_builders'][] = 'entity_redirect_bundle_builder';
    }
    $info = $form_state->getBuildInfo();
    if (isset($info['base_form_id']) && in_array($info['base_form_id'], $entity_forms)) {
        $info = $form_state->getBuildInfo();
        if ($info['base_form_id'] === 'node_form') {
            $type = 'node_type';
        }
        elseif ($info['base_form_id'] === 'media_form') {
            // If media_entity is enabled, the entity type we are looking for is
            // 'media_bundle', if not, it means we are using the core media module
            // so the entity type is 'media_type'.
            $type = \Drupal::moduleHandler()->moduleExists('media_entity') ? 'media_bundle' : 'media_type';
        }
        elseif ($info['base_form_id'] === 'taxonomy_term_form') {
            $type = 'taxonomy_vocabulary';
        }
        elseif ($info['base_form_id'] === 'commerce_product_form') {
            $type = 'commerce_product_type';
        }
        $entity = $form_state->getFormObject()
            ->getEntity();
        $bundle = $entity->bundle();
        $entity_type = \Drupal::service('entity_type.manager')->getStorage($type)
            ->load($bundle);
        if (!$entity->isNew() && !$entity_type->getThirdPartySetting('entity_redirect', 'redirect_edit')) {
            return;
        }
        $form['actions']['publish']['#submit'][] = 'entity_redirect_submit';
        $form['actions']['submit']['#submit'][] = 'entity_redirect_submit';
    }
}