Implements hook_form_FORM_ID_alter().

Add the personalizable settings to an individual user's account page.

See also

\Drupal\user\ProfileForm::form()

File

./entity_redirect.module, line 212

Code

function entity_redirect_form_user_form_alter(&$form, FormStateInterface $form_state) {
    // Ensure user has necessary permission.
    if (!\Drupal::currentUser()->hasPermission('administer personalized redirect options')) {
        return;
    }
    $user_id = $form_state->getFormObject()
        ->getEntity()
        ->id();
    if (!isset($form['entity_options'])) {
        $form['entity_options'] = [
            '#type' => 'details',
            '#title' => t('Entity Personalization'),
            '#tree' => TRUE,
            '#open' => FALSE,
            '#weight' => 10,
        ];
    }
    // 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'.
    if (\Drupal::moduleHandler()->moduleExists('media_entity') == TRUE) {
        $types = [
            'node_type' => [
                'title' => t('Content'),
                'access' => 'node',
            ],
            'media_bundle' => [
                'title' => t('Media'),
                'access' => 'media',
            ],
            'taxonomy_vocabulary' => [
                'title' => t('Vocabulary'),
                'access' => 'taxonomy_term',
            ],
            'commerce_product_type' => [
                'title' => t('Commerce Product'),
                'access' => 'commerce_product',
            ],
        ];
    }
    else {
        $types = [
            'node_type' => [
                'title' => t('Content'),
                'access' => 'node',
            ],
            'media_type' => [
                'title' => t('Media'),
                'access' => 'media',
            ],
            'taxonomy_vocabulary' => [
                'title' => t('Vocabulary'),
                'access' => 'taxonomy_term',
            ],
            'commerce_product_type' => [
                'title' => t('Commerce Product'),
                'access' => 'commerce_product',
            ],
        ];
    }
    foreach ($types as $type => $type_data) {
        // Ensure that the entity type exists for this install.
        if (!\Drupal::entityTypeManager()->hasDefinition($type)) {
            continue;
        }
        if (!isset($form['entity_options'][$type])) {
            $form['entity_options'][$type] = [
                '#title' => $type_data['title'],
                '#type' => 'details',
                '#open' => TRUE,
            ];
        }
        foreach (\Drupal::entityTypeManager()->getStorage($type)
            ->loadMultiple() as $bundle) {
            // Filter by entity access permission.
            if (!\Drupal::entityTypeManager()->getAccessControlHandler($type_data['access'])
                ->createAccess($bundle->id())) {
                continue;
            }
            // Filter by entity settings.
            if (!$bundle->getThirdPartySetting('entiry_redirect', 'personalizable', TRUE)) {
                continue;
            }
            $label = $bundle->label();
            $id = $bundle->id();
            $defaults = $bundle->getThirdPartySetting('entity_redirect', "personalization");
            if (!isset($defaults[$user_id])) {
                $defaults[$user_id] = [
                    'destination' => 'default',
                ];
            }
            if (!isset($form['entity_options'][$type][$id])) {
                $form['entity_options'][$type][$id] = [
                    '#title' => $label,
                    '#type' => 'details',
                    '#tree' => TRUE,
                    '#open' => TRUE,
                ];
            }
            $form['entity_options'][$type][$id]['destination'] = [
                '#type' => 'select',
                '#title' => t('Redirect destination'),
                '#options' => [
                    'default' => t('- Default -'),
                    'add_form' => t('Add Form'),
                    'created' => t('Created %entity_label', [
                        '%entity_label' => $label,
                    ]),
                    'url' => t('Local Url'),
                    'external' => t('External Url'),
                ],
                '#default_value' => $defaults[$user_id]['destination'],
            ];
            $form['entity_options'][$type][$id]['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($defaults[$user_id]['url']) ? $defaults[$user_id]['url'] : '',
                '#convert_path' => PathElement::CONVERT_NONE,
                '#states' => [
                    'visible' => [
                        "select[name='entity_options[{$type}][{$id}][destination]']" => [
                            'value' => 'url',
                        ],
                    ],
                ],
            ];
            $form['entity_options'][$type][$id]['external'] = [
                '#type' => 'url',
                '#title' => t('External Destination Url'),
                '#description' => t('Enter a fully qualified url such as https://example.com/page.'),
                '#default_value' => isset($defaults[$user_id]['external']) ? $defaults[$user_id]['external'] : '',
                '#access' => \Drupal::currentUser()->hasPermission('set external entity redirects'),
                '#states' => [
                    'visible' => [
                        "select[name='entity_options[{$type}][{$id}][destination]']" => [
                            'value' => 'external',
                        ],
                    ],
                ],
            ];
        }
    }
    $form['actions']['submit']['#submit'][] = 'entity_redirect_user_profile_form_submit';
}