How to change the Profile Title?

Created on 19 January 2023, almost 2 years ago
Updated 12 April 2024, 7 months ago

Problem/Motivation

The Profile Title cannot be customized/overridden. It isn't user friendly to see (e.g) "Contributor #1" on the Profile Page, User Account Page, in the browser tab, etc.

Steps to reproduce

Create a Profile Type, then create a Profile. The Entity Title becomes [Profile-Type] #[#]. For example, I created a "Contributor" Profile Type. So my first Profile that I created is called "Contributor #1". That's not at all what I want the content creators or the site visitors to see.

I've successfully hidden the text on the profile page by simply removing the Page Title Block from /profile/* pages, but there are the other places it shows up to deal with: the list of profiles on the User Account page and the browser tab being the two most prominent to me. Maybe there are more.

Proposed resolution

Give site builders a way to edit/override/define what the Title should be. In my case, it would be ideal for the Contributor Profile Type to have a Title of the Contributor's real name. Or, something like "Contributor: [First Name] [Last Name]

If I'm missing the way to do this that already exists, I'm happy to be pointed in the right direction. If not, please consider this to be a feature request.

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Fixed

Version

1.0

Component

Miscellaneous

Created by

🇨🇦Canada michaelschutz

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇦Canada ryanrobinson_wlu

    +1 that I'd like to see this feature added to the module.

    In the meantime in case it helps, here's my notes on how I changed the title in a few places: https://ryanrobinson.technology/websites/drupal/override-title-tag-profi...

  • 🇪🇸Spain eidoscom

    What I did to change the label is:

    1. Create a module named profile_label
    2. Create profile_label.module file with this content:

    <?php
    
    /**
     * Implements hook_entity_type_alter().
     */
    function profile_label_entity_type_alter(array &$entity_types) {
        // Reemplaza la clase de entidad Profile original con la personalizada.
        $entity_types['profile']->setClass('Drupal\profile_label\Entity\Profile');
      }
      

    3. Create the src/Entity/Profile.php file with this content:

    <?php
    
    namespace Drupal\profile_label\Entity;
    
    use Drupal\profile\Entity\Profile as BaseProfile;
    
    /**
     * Defines the custom profile entity class.
     */
    class Profile extends BaseProfile {
    
      /**
       * {@inheritdoc}
       */
      public function label() {
        // Customize the logic to generate the profile label here.
         // You can call the original implementation using parent::label() if necessary.
         // For example:
         // $original_label = parent::label();
         // Customize $original_label to your needs.
        
         // Your custom logic to generate the profile label here.
        $first_name = $this->get('field_nom')->getString();
        $last_name = $this->get('field_primer_cognom')->getString();
        //$curs = "(" . $this->get('field_curs')->getString() .")";
    
        if($first_name != "" && $last_name != "") {
          $custom_label = "$first_name $last_name";
          return $custom_label;
        }
      }
    
    }
    

    You can modify the logic to make your functionality. In my case, field_nom, field_cognom and field_curs are fields attached to the profile entity.

    Hope this can help someone.
    Thanks!

  • Hi, how would I make the label link to the profile as well?

    Thanks for providing the code.

  • 🇪🇸Spain eidoscom

    Hello @london6339, the code that I shared has nothing to do with it.

    Not 100% sure but as the label is not stored in the DDBB, you can not access the label using Views.

    Nevertheless, you can easily make a link with the label inside views by using the fields that you want for create the label and then link it to the profile.

  • Status changed to Needs review about 1 year ago
  • 🇮🇳India ravi kant Jaipur

    The easiest way to solve this issue is preprocessor methods as below.

    /**
     * Implements hook_preprocess_HOOK() for page_title template.
     */
    function THEME-NAME_preprocess_page_title(&$variables) {
      $route_match = \Drupal::routeMatch();
      if ($route_match->getRouteName() === 'entity.profile.canonical') {
        $variables['title'] = $variables['user']->getDisplayName();
      }
    }
    
  • Status changed to Fixed about 1 year ago
  • 🇺🇸United States dww

    Re: #6: But the profile label appears in many places, not just on that canonical route. So preprocessing the page title only handles that one page, not things like the list at the admin/people/profiles page.

    The officially supported way of handling this (which Drupal Commerce uses for the 'customer' profile types to manage billing and shipping addresses) is with an event subscriber that listens to the ProfileEvents::PROFILE_LABEL event and alters the label.

    See commerce/modules/order/src/EventSubscriber/ProfileLabelSubscriber.php and commerce/modules/order/commerce_order.services.yml for a working example.

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

  • Status changed to Fixed 7 months ago
  • @eidoscom

    Your module has been working great, but I've ran into some issues now.

    When I go to /admin/people/profiles, it throws an error that my field is unknown. Perhaps this is caused by the Commerce module also using the Profile module?

    InvalidArgumentException: Field field_first_name is unknown. in Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (line 616 of /var/www/html/core/lib/Drupal/Core/Entity/ContentEntityBase.php).
    
    #0 /var/www/html/core/lib/Drupal/Core/Entity/ContentEntityBase.php(597): Drupal\Core\Entity\ContentEntityBase->getTranslatedField()
    #1 /var/www/html/modules/contrib/profile_label/src/Entity/Profile.php(23): Drupal\Core\Entity\ContentEntityBase->get()
    #2 /var/www/html/core/lib/Drupal/Core/Entity/EntityBase.php(257): Drupal\profile_label\Entity\Profile->label()
    #3 /var/www/html/modules/contrib/profile/src/ProfileListBuilder.php(118): Drupal\Core\Entity\EntityBase->toLink()
    #4 /var/www/html/core/lib/Drupal/Core/Entity/EntityListBuilder.php(242): Drupal\profile\ProfileListBuilder->buildRow()
    #5 /var/www/html/core/lib/Drupal/Core/Entity/Controller/EntityListController.php(23): Drupal\Core\Entity\EntityListBuilder->render()
    #6 [internal function]: Drupal\Core\Entity\Controller\EntityListController->listing()
    #7 /var/www/html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
    #8 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #9 /var/www/html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext()
    #10 /var/www/html/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
    #11 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #12 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
    #13 /var/www/html/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
    #14 /var/www/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
    #15 /var/www/html/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
    #16 /var/www/html/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle()
    #17 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\big_pipe\StackMiddleware\ContentLength->handle()
    #18 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
    #19 /var/www/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle()
    #20 /var/www/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
    #21 /var/www/html/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
    #22 /var/www/html/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
    #23 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(704): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
    #24 /var/www/html/index.php(19): Drupal\Core\DrupalKernel->handle()
    #25 {main}

    Thanks in advance.

Production build 0.71.5 2024