The name.formatter service documented on the module's home page looks great, but shouldn't it also be (and maybe it is?) possible to choose a format directly from a field, something that would look like this:

  $full_name = $node->get('field_name')->format('full');

And a version of that for Twig as well would be even nicer!

Issue fork name-3076553

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

mlncn created an issue. See original summary.

bluegeek9’s picture

Title: Add/document choosing a format from code or twig » Add format method to NameItem.php
Component: Documentation » Code
Category: Support request » Feature request
Status: Active » Postponed

The closest you can get today is calling name.formatter yourself:

$formatted = \Drupal::service('name.formatter')
  ->format($node->get('field_name')->first()->filteredArray(), 'full');

filteredArray() (line 163 of NameItem.php) already returns only the components that are active for that field, so it's the right source. But you need to reach out to the service manually.

Twig
Templates like name-item.html.twig expose a pre-rendered formatted_name variable built during template_preprocess_name_item(), but that uses a fixed format chosen by the field formatter display settings. You can't ask for 'full' vs 'short' at template time. There is no Twig extension at all.

I am marking the issue as postponed because adding a new method should be done in a major release.

bluegeek9’s picture

Status: Postponed » Active
bluegeek9’s picture

Assigned: Unassigned » bluegeek9

bluegeek9’s picture

Assigned: bluegeek9 » Unassigned
Status: Active » Needs review

bluegeek9 changed the visibility of the branch 8.x-1.x to hidden.

  • bluegeek9 committed 069b3d8e on 8.x-1.x
    feat: #3076553 Add format method to NameItem.php
    
bluegeek9’s picture

Status: Needs review » Fixed
//www.flaticon.com/free-icons/thank-you Thank you for your contribution! Your continued support makes this project sustainable.
There are multiple ways to show appreciation for the work contributed to this project including:

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.

Status: Fixed » Closed (fixed)

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