Problem/Motivation

upgrade status reports:

File name Line Error
web/modules/contrib/show_email/src/Plugin/Field/FieldFormatter/ShowEmailAddress.php 72 Call to deprecated function user_role_names(). Deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use Drupal\user\Entity\Role::loadMultiple() and, if necessary, an inline implementation instead.

File name Line Error
web/modules/contrib/show_email/show_email.info.yml 5 Value of core_version_requirement: ^8 || ^9 || ^10 is not compatible with the next major version of Drupal core. See https://drupal.org/node/3070687.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork show_email-3485932

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

maxilein created an issue. See original summary.

sarwan_verma made their first commit to this issue’s fork.

sarwan_verma’s picture

Status: Active » Needs review

Hi,
I have fixed all the Drupal 11 compatibility issues and created MR!2.
Kindly review it.
Thanks!

sarigaraghunath’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new26.24 KB

MR2 is working fine in both d10 and d11

maxilein’s picture

The patch does not apply on the latest dev odr stable versions (as of 20 Jan 2024 at 20:16 CET)

maxilein’s picture

Sorry it applied to the dev version correctly.

richarddavies’s picture

StatusFileSize
new1.39 KB

Here's a patch that is identical to MR2, but it will work on the 1.2 release (so you don't have to load the dev version--which ironically is currently the same as v1.2).

jaykainthola’s picture

Patch #8 is working fine with Drupal 11

sautiwari’s picture

Drupal 11 compatibility note:

The current version of show_email uses user_role_names(), which was deprecated in Drupal 10.2 and removed in Drupal 11.

For projects upgrading to Drupal 11, we replaced the module with a lightweight custom Field Formatter that provides similar functionality without deprecated APIs.

Key changes:
- Removed user_role_names()
- Used Role::loadMultiple() instead
- Avoided procedural user_load_* functions
- Relied on $items->getEntity() for performance
- Added proper cache context (user.roles)

Below is a Drupal 11 compatible formatter implementation:

<?php

namespace Drupal\show_email\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\user\Entity\Role;

/**
 * Plugin implementation of the 'show_email_address' formatter.
 *
 * @FieldFormatter(
 *   id = "show_email_address",
 *   label = @Translation("Show email address"),
 *   field_types = {
 *     "email"
 *   }
 * )
 */
class ShowEmailAddress extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'hide_user_one' => TRUE,
      'hide_per_role' => [],
      'email_mailto' => FALSE,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];

    $summary[] = $this->getSetting('hide_user_one')
      ? $this->t('User 1 is hidden.')
      : $this->t('User 1 is visible.');

    $summary[] = $this->getSetting('email_mailto')
      ? $this->t('Mailto link enabled.')
      : $this->t('Plain text email.');

    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements = parent::settingsForm($form, $form_state);

    // Drupal 11 compatible role loading.
    $roles = Role::loadMultiple();
    $role_options = [];

    foreach ($roles as $role) {
      $role_options[$role->id()] = $role->label();
    }

    $elements['hide_user_one'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Hide User 1'),
      '#default_value' => $this->getSetting('hide_user_one'),
    ];

    $elements['hide_per_role'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Hide per role'),
      '#options' => $role_options,
      '#default_value' => $this->getSetting('hide_per_role'),
    ];

    $elements['email_mailto'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable mailto link'),
      '#default_value' => $this->getSetting('email_mailto'),
    ];

    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];

    // Formatter applies to user entity.
    $user = $items->getEntity();

    // Hide User 1 if enabled.
    if ($this->getSetting('hide_user_one') && $user->id() == 1) {
      return $elements;
    }

    // Hide based on role.
    $roles_to_hide = array_filter($this->getSetting('hide_per_role'));
    if (!empty(array_intersect($roles_to_hide, $user->getRoles()))) {
      return $elements;
    }

    foreach ($items as $delta => $item) {
      $email = $item->value;

      $elements[$delta] = [
        '#type' => 'markup',
        '#markup' => $this->getSetting('email_mailto')
          ? '<a href="mailto:' . $email . '">' . $email . '</a>'
          : $email,
        '#cache' => [
          'contexts' => ['user.roles'],
        ],
      ];
    }

    return $elements;
  }

}

During Drupal 11 upgrade, we removed deprecated procedural functions (user_role_names(), user_load_by_mail()), eliminated unnecessary .module and schema files, simplified role filtering logic, and modernized the formatter to use entity-based APIs and proper cache contexts.

  • abdulaziz zaid committed 2d37e122 on 3.0.x
    Issue #3485932 by sarwan_verma, maxilein, sarigaraghunath, richarddavies...
abdulaziz zaid’s picture

Status: Reviewed & tested by the community » Fixed

Fixed in 3.0.0. Supports Drupal 10 and 11.

To upgrade: composer require 'drupal/show_email:^3.0'

Thanks, everyone, for your contributions.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

abdulaziz zaid’s picture

Version: 8.x-1.x-dev » 3.0.x-dev

Status: Fixed » Closed (fixed)

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