- 🇨🇦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 12:31pm 12 October 2023 - 🇮🇳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 12:02am 15 November 2023 - 🇺🇸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
andcommerce/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 11:26pm 12 April 2024 @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.