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
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
Comment #2
bluegeek9 commentedThe closest you can get today is calling name.formatter yourself:
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.
Comment #3
bluegeek9 commentedAn issue to add a twig extension exists. #3553196: Add Twig filter to format name fields using NameFormatterInterface
Comment #4
bluegeek9 commentedComment #5
bluegeek9 commentedComment #7
bluegeek9 commentedComment #10
bluegeek9 commented