Problem/Motivation

I would like to extend your module. Actually I use "drupal/profile" module. Lot of user information is stored in user profile entity.
I want to extend your enforce_user_fields_have_unfilled_required_fields function to check the profiles.
It will be more flexible to add custom code.

Comments

kecsot created an issue. See original summary.

kecsot’s picture

How to use the new hook?

/**
 * Implements hook_enforce_user_fields_have_unfilled_required_fields().
 */
function MY_MODULE_enforce_user_fields_have_unfilled_required_fields(&$result, \Drupal\Core\Session\AccountInterface $account) {
   if (!$result) {
    $foo_service = \Drupal::service("foo");
    $result = $foo_service->isFooBarFilled($account);
  }
}
kecsot’s picture

Issue summary: View changes
kecsot’s picture

Version: 2.0.0 » 8.x-1.7

sorry, my bad

kecsot’s picture

Changes in this patch:

enforce_user_fields.api.php
- $result = $foo_service->isFooBarFilled($account);
+ $result = !$foo_service->isFooBarFilled($account);

kecsot’s picture

alter for profile module:

/**
 * Implements hook_enforce_user_fields_have_unfilled_required_fields().
 */
function MY_MODULE_enforce_user_fields_have_unfilled_required_fields(&$result, \Drupal\Core\Session\AccountInterface $account) {
   if(!$result){
    /** @var ProfileStorageInterface $profile_storage */
    $profile_storage = \Drupal::service('entity_type.manager')->getStorage('profile');

    $profile_types = \Drupal::entityQuery('profile_type')->execute();
    $profile_type_entities = ProfileType::loadMultiple($profile_types);
    $user_roles = $account->getRoles();
    foreach ($profile_type_entities as $type){
      $actual_profile_id = $type->id();
      $is_account_have_role = in_array($actual_profile_id, $user_roles);

      if($is_account_have_role && $profile = $profile_storage->loadByUser($account, $actual_profile_id)){

        $field_names = array_keys($profile->getFields());

        foreach ($field_names as $field_name){
          $field_definition = $profile->get($field_name)->getFieldDefinition();
          if ($field_definition instanceof ThirdPartySettingsInterface) {
            if ($field_definition->isRequired() && $profile->get($field_name)->isEmpty()) {
              $result = TRUE;
              break;
            }
          }
        }
      }
      if($result){break;}
    }
  }
}
kecsot’s picture

Version: 8.x-1.7 » 2.0.0
kecsot’s picture

Status: Active » Needs review

  • cosolom committed f9d0d247 on 2.0.x
    Issue #3285557 by kecsot: Allow to alter...
cosolom’s picture

Version: 2.0.0 » 2.0.x-dev
Status: Needs review » Fixed

Thanks all. BTW need to use alter and not invokeAll for such cases. Will be released shortly

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.