Skip to content
Snippets Groups Projects
Verified Commit 55e738a6 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3479141 by nicxvan, oily, berdir, dww, ghost of drupal past, longwave,...

Issue #3479141 by nicxvan, oily, berdir, dww, ghost of drupal past, longwave, quietone: Implement FormAlter attribute
parent bc40a08c
No related branches found
No related tags found
3 merge requests!5423Draft: Resolve #3329907 "Test2",!3478Issue #3337882: Deleted menus are not removed from content type config,!579Issue #2230909: Simple decimals fail to pass validation
Pipeline #489671 passed
Pipeline: drupal

#489679

    Pipeline: drupal

    #489676

      Pipeline: drupal

      #489673

        <?php
        declare(strict_types=1);
        namespace Drupal\Core\Hook\Attribute;
        /**
        * Hook attribute for FormAlter.
        *
        * @see hook_form_alter().
        */
        #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
        class FormAlter extends Hook {
        /**
        * {@inheritdoc}
        */
        public const string PREFIX = 'form';
        /**
        * {@inheritdoc}
        */
        public const string SUFFIX = 'alter';
        /**
        * Constructs a FormAlter attribute object.
        *
        * @param string $form_id
        * (optional) The ID of the form that this implementation alters.
        * If this is left blank then `form_alter` is the hook that is registered.
        * @param string $method
        * (optional) The method name. If this attribute is on a method, this
        * parameter is not required. If this attribute is on a class and this
        * parameter is omitted, the class must have an __invoke() method, which is
        * taken as the hook implementation.
        * @param string|null $module
        * (optional) The module this implementation is for. This allows one module
        * to implement a hook on behalf of another module. Defaults to the module
        * the implementation is in.
        */
        public function __construct(
        string $form_id = '',
        public string $method = '',
        public ?string $module = NULL,
        ) {
        parent::__construct($form_id, $method, $module);
        }
        }
        ......@@ -96,6 +96,7 @@
        */
        #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
        class Hook implements HookAttributeInterface {
        /**
        * The hook prefix such as `form`.
        *
        ......
        <?php
        namespace Drupal\contact\Hook;
        use Drupal\Core\Config\ConfigFactoryInterface;
        use Drupal\Core\Form\FormStateInterface;
        use Drupal\Core\Hook\Attribute\FormAlter;
        use Drupal\Core\Session\AccountInterface;
        use Drupal\Core\StringTranslation\StringTranslationTrait;
        use Drupal\user\UserDataInterface;
        /**
        * Form hook implementations for Contact module.
        */
        class ContactFormHooks {
        use StringTranslationTrait;
        public function __construct(
        protected readonly AccountInterface $currentUser,
        protected readonly UserDataInterface $userData,
        protected readonly configFactoryInterface $configFactory,
        ) {}
        /**
        * Implements hook_form_FORM_ID_alter() for \Drupal\user\ProfileForm.
        *
        * Add the enable personal contact form to an individual user's account page.
        *
        * @see \Drupal\user\ProfileForm::form()
        */
        #[FormAlter('user_form')]
        public function formUserFormAlter(&$form, FormStateInterface $form_state) : void {
        $form['contact'] = [
        '#type' => 'details',
        '#title' => $this->t('Contact settings'),
        '#open' => TRUE,
        '#weight' => 5,
        ];
        $account = $form_state->getFormObject()->getEntity();
        if (!$this->currentUser->isAnonymous() && $account->id()) {
        $account_data = $this->userData->get('contact', $account->id(), 'enabled');
        }
        $form['contact']['contact'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Personal contact form'),
        '#default_value' => $account_data ?? $this->configFactory->getEditable('contact.settings')->get('user_default_enabled'),
        '#description' => $this->t('Allow other users to contact you via a personal contact form which keeps your email address hidden. Note that some privileged users such as site administrators are still able to contact you even if you choose to disable this feature.'),
        ];
        $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';
        }
        /**
        * Implements hook_form_FORM_ID_alter() for \Drupal\user\AccountSettingsForm.
        *
        * Adds the default personal contact setting on the user settings page.
        */
        #[FormAlter('user_admin_settings')]
        public function formUserAdminSettingsAlter(&$form, FormStateInterface $form_state) : void {
        $form['contact'] = [
        '#type' => 'details',
        '#title' => $this->t('Contact settings'),
        '#open' => TRUE,
        '#weight' => 0,
        ];
        $form['contact']['contact_default_status'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Enable the personal contact form by default for new users'),
        '#description' => $this->t('Changing this setting will not affect existing users.'),
        '#default_value' => $this->configFactory->getEditable('contact.settings')->get('user_default_enabled'),
        ];
        // Add submit handler to save contact configuration.
        $form['#submit'][] = 'contact_form_user_admin_settings_submit';
        }
        }
        ......@@ -3,7 +3,6 @@
        namespace Drupal\contact\Hook;
        use Drupal\contact\Plugin\rest\resource\ContactMessageResource;
        use Drupal\Core\Form\FormStateInterface;
        use Drupal\Core\StringTranslation\StringTranslationTrait;
        use Drupal\user\Entity\User;
        use Drupal\Core\Url;
        ......@@ -172,57 +171,6 @@ public function mail($key, &$message, $params): void {
        }
        }
        /**
        * Implements hook_form_FORM_ID_alter() for \Drupal\user\ProfileForm.
        *
        * Add the enable personal contact form to an individual user's account page.
        *
        * @see \Drupal\user\ProfileForm::form()
        */
        #[Hook('form_user_form_alter')]
        public function formUserFormAlter(&$form, FormStateInterface $form_state) : void {
        $form['contact'] = [
        '#type' => 'details',
        '#title' => $this->t('Contact settings'),
        '#open' => TRUE,
        '#weight' => 5,
        ];
        $account = $form_state->getFormObject()->getEntity();
        if (!\Drupal::currentUser()->isAnonymous() && $account->id()) {
        $account_data = \Drupal::service('user.data')->get('contact', $account->id(), 'enabled');
        }
        $form['contact']['contact'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Personal contact form'),
        '#default_value' => $account_data ?? \Drupal::config('contact.settings')->get('user_default_enabled'),
        '#description' => $this->t('Allow other users to contact you via a personal contact form which keeps your email address hidden. Note that some privileged users such as site administrators are still able to contact you even if you choose to disable this feature.'),
        ];
        $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';
        }
        /**
        * Implements hook_form_FORM_ID_alter() for \Drupal\user\AccountSettingsForm.
        *
        * Adds the default personal contact setting on the user settings page.
        */
        #[Hook('form_user_admin_settings_alter')]
        public function formUserAdminSettingsAlter(&$form, FormStateInterface $form_state) : void {
        $form['contact'] = [
        '#type' => 'details',
        '#title' => $this->t('Contact settings'),
        '#open' => TRUE,
        '#weight' => 0,
        ];
        $form['contact']['contact_default_status'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Enable the personal contact form by default for new users'),
        '#description' => $this->t('Changing this setting will not affect existing users.'),
        '#default_value' => \Drupal::configFactory()->getEditable('contact.settings')->get('user_default_enabled'),
        ];
        // Add submit handler to save contact configuration.
        $form['#submit'][] = 'contact_form_user_admin_settings_submit';
        }
        /**
        * Implements hook_rest_resource_alter().
        */
        ......
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Please register or to comment