0

I have the module :

https://www.drupal.org/project/profile

https://www.drupal.org/project/twig_tweak

Here is the twig file of my comments :

{#
/**
 * @file
 * Default theme implementation for comments.
 *
 * Available variables:
 * - author: Comment author. Can be a link or plain text.
 * - content: The content-related items for the comment display. Use
 *   {{ content }} to print them all, or print a subset such as
 *   {{ content.field_example }}. Use the following code to temporarily suppress
 *   the printing of a given child element:
 *   @code
 *   {{ content|without('field_example') }}
 *   @endcode
 * - created: Formatted date and time for when the comment was created.
 *   Preprocess functions can reformat it by calling format_date() with the
 *   desired parameters on the 'comment.created' variable.
 * - changed: Formatted date and time for when the comment was last changed.
 *   Preprocess functions can reformat it by calling format_date() with the
 *   desired parameters on the 'comment.changed' variable.
 * - permalink: Comment permalink.
 * - submitted: Submission information created from author and created
 *   during template_preprocess_comment().
 * - user_picture: The comment author's profile picture.
 * - status: Comment status. Possible values are:
 *   unpublished, published, or preview.
 * - title: Comment title, linked to the comment.
 * - attributes: HTML attributes for the containing element.
 *   The attributes.class may contain one or more of the following classes:
 *   - comment: The current template type; for instance, 'theming hook'.
 *   - by-anonymous: Comment by an unregistered user.
 *   - by-{entity-type}-author: Comment by the author of the parent entity,
 *     eg. by-node-author.
 *   - preview: When previewing a new or edited comment.
 *   The following applies only to viewers who are registered users:
 *   - unpublished: An unpublished comment visible only to administrators.
 * - title_prefix: Additional output populated by modules, intended to be
 *   displayed in front of the main title tag that appears in the template.
 * - title_suffix: Additional output populated by modules, intended to be
 *   displayed after the main title tag that appears in the template.
 * - content_attributes: List of classes for the styling of the comment content.
 * - title_attributes: Same as attributes, except applied to the main title
 *   tag that appears in the template.
 * - threaded: A flag indicating whether the comments are threaded or not.
 *
 * These variables are provided to give context about the parent comment (if
 * any):
 * - comment_parent: Full parent comment entity (if any).
 * - parent_author: Equivalent to author for the parent comment.
 * - parent_created: Equivalent to created for the parent comment.
 * - parent_changed: Equivalent to changed for the parent comment.
 * - parent_title: Equivalent to title for the parent comment.
 * - parent_permalink: Equivalent to permalink for the parent comment.
 * - parent: A text string of parent comment submission information created from
 *   'parent_author' and 'parent_created' during template_preprocess_comment().
 *   This information is presented to help screen readers follow lengthy
 *   discussion threads. You can hide this from sighted users using the class
 *   visually-hidden.
 *
 * These two variables are provided for context:
 * - comment: Full comment object.
 * - entity: Entity the comments are attached to.
 *
 * @see template_preprocess_comment()
 *
 * @ingroup themeable
 */
#}

<article{{ attributes.addClass('js-comment comment--field-article-commentaire') }}>

  <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bs-region-comment">

    <div class="col-xs-3 col-sm-3 col-md-2 col-lg-2 bs-left">
      <div class="row">

        {{ drupal_entity('user', comment.uid.target_id, 'comment_teaser_relation') }}
        {{ author }}

        <div class="bs-field-link">
          <a href="{{ ??? URL PROFILE USER ??? }}" title="{{ ??? USERNAME ??? }}"></a>
        </div>

      </div>
    </div>

    <div class="col-xs-9 col-sm-9 col-md-10 col-lg-10 bs-right">
     <b><i>{{ created }}</i></b>
     {{ content.field_article_commentaire }}
     {{ content.links }}
     {{ content.flag_report_comment }}
    </div>

  </div>

</article>

I want to display instead of :

{{ ??? URL PROFILE USER ??? }}

The url of the profile (profil_personnel) of the author of the comment.

On the comment_teaser_relation view of the user account, there is a profile field personnel that I activated.

Here is the twig file of the user account :

{#
/**
 * @file
 * Default theme implementation to present all user data.
 *
 * This template is used when viewing a registered user's page,
 * e.g., example.com/user/123. 123 being the user's ID.
 *
 * Available variables:
 * - content: A list of content items. Use 'content' to print all content, or
 *   print a subset such as 'content.field_example'. Fields attached to a user
 *   such as 'user_picture' are available as 'content.user_picture'.
 * - attributes: HTML attributes for the container element.
 * - user: A Drupal User entity.
 *
 * @see template_preprocess_user()
 *
 * @ingroup themeable
 */
#}
<article{{ attributes.addClass('user--comment-teaser-relation') }}>

    {{ content.profile_personnel }}
    {{ content.user_picture }}

</article>

If I enter the code {{ content.profile_personnel }} in the twig file of the user display, the profile is displayed.

How to display only the profile link ?

And how to display the profile link of the author of the comment, in the comments twig ?

Here is the twig of the profile if it can help :

{#
/**
 * @file
 *
 * Default theme implementation for profiles.
 *
 * Available variables:
 * - content: Items for the content of the profile.
 *   Use 'content' to print them all, or print a subset such as
 *   'content.title'. Use the following code to exclude the
 *   printing of a given child element:
 *   @code
 *   {{ content|without('title') }}
 *   @endcode
 * - attributes: HTML attributes for the wrapper.
 * - profile: The profile object.
 * - url: The profile URL, if available.
 *
 * @ingroup themeable
 */
#}

enter image description here

  • This might help: drupal.org/docs/8/theming/twig/functions-in-twig-templates -- if you know the id, you can generate a link in twig. – Kevin Apr 18 '18 at 14:40
  • @Kevin Thank you, but I do not master at all twig – user84727 Apr 18 '18 at 15:10
  • If you just want to link to the user profile page, you can do <a href="{{ path('entity.user.canonical', {'user': user.id}) }}">{{ 'View user profile'|t }}</a> - in your case I am guessing the user.id is instead comment.uid.target_id – Kevin Apr 18 '18 at 15:14
  • @Kevin In fact I am talking about the profile (module profile). Who is linked to the user account. See the screenshot at the end of my question. – user84727 Apr 18 '18 at 15:19
  • The core module? – Kevin Apr 18 '18 at 15:20
0

To link to this in the twig file, you can use the path function like so:

<a href="{{ path('entity.profile.canonical', {'profile': comment.uid.content.profile_personnel.target_id}) }}">{{ 'View user profile'|t }}</a>

I am guessing that comment.uid.content.profile_personnel.target_id returns the entity id of the profile. This will create a /profile/{id} href where {id} is the profile id.

I don't use twig tweak, but I assume it would be:

{{ drupal_entity('profile', comment.uid.content.profile_personnel.target_id) }}

But I think that would embed the profile, not link to it, which is why you want something like the path function.

  • I have an error message Symfony\Component\Routing\Exception\InvalidParameterException : Parameter "profile" for route "entity.profile.canonical" must match "[^/]++" ("" given) to generate a corresponding URL. dans Drupal\Core\Routing\UrlGenerator->doGenerate() (ligne 204 de /home/www.s1biose.com/public_html/web/core/lib/Drupal/Core/Routing/UrlGenerator.php). – user84727 Apr 18 '18 at 15:40
  • That’s because the variable is not producing an id value, I don’t know what it would be based on your setup. You can test it by passing it the actual number just to see it work, then debug the variables to figure out the correct reference. – Kevin Apr 18 '18 at 15:46
  • In the profile twig, there is {{url}} that displays the url of the profile. How to recover this in the comments ? – user84727 Apr 18 '18 at 15:48
  • Those variables are only available in that file when it’s rendered. – Kevin Apr 18 '18 at 15:52
  • I can make the complete profile in the user twig with {{ content.profile_personnel }} so how to display the profile url in the user twig ? – user84727 Apr 18 '18 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy