1

I'm wanting to modify the contents of some fields at display time so looked at hook_node_view(), however, the $build array is not what I'm expecting, if I dump it with var_dump it is thousands of lines long and not in the format I'm expecting a render array to be, it includes all sorts of exciting things like SQL connection objects which are of no use to me but there is no obvious set of elements that I'd be expecting to see given the Drupal documentation. If I drill down the array, I can find the data I need, however, I can't see how to get this sorted in the correct display order.

If I add a simple tweak like

$build['title'] = ['#markup' => "Hello World"];

the node title is rendered correctly as the new value so I know I'm roughly in the right place.

Question, how can I get at the fields collection after extraction from the database and sorting into display order but before it hits the template as I need to be able to make changes at this point as future development will require potential output via differing templates.

  • 1
    For D8 use the Devel submodule Kint and then print your vars wih ksm($var). And don't click the plus icon. For D7 use Devel's dpm($var). – leymannx Feb 5 at 18:18
  • 2
    Question, how can I get at the fields collection after extraction from the database and sorting into display order but before it hits the template isnt this already done via {{ content }}? That render array has the fields in the proper order and display as set from the Manage Display tab on an entity. – Kevin Feb 5 at 18:28
  • ksm() looks good, however, the $build array is so large I get a memory exhausted even upping to 2Gb and there's only 18 custom fields in this content type. Any other suggestions? – Alex Monaghan Feb 6 at 13:40
  • If I add {{ kint(page.content) }} to my page.html.twig template, I still get a huge array of data and not a simple render array as the api documentation seems to suggest. – Alex Monaghan Feb 6 at 14:01
0

Suggestions

  • Get the node from the $build (it should be in there somewhere) and build custom content for your field like $build['custom_field'] = [...].
  • Override the twig template for the field - this will let you alter the presentation of a particular field. Ex: implement node--field-custom-field.html.twig based on field.html.twig and alter the output of the field there.
  • Override the twig template for the entire node. Ex: implement node--article.html.twig based on node.html.twig and alter the output of the node there.

Let me know if any of these helps. More details on exactly what or why you want to alter stuff will be helpful.

  • Maybe OP also might want to try a preprocess hook, like hook_preprocess_field maybe. – leymannx Feb 5 at 18:45
  • Thanks, it's finding the data within $build that's the initial problem as the variable contains such a huge data structure. What I'm trying to do initially is to collect data & remove some of it from a number of fields, and then to use this collected data in another field (the particular use case at the moment is collecting footnotes from a number of fields and creating them for the whole document) – Alex Monaghan Feb 6 at 13:42
0

I wrestled with this far longer than I care to admit... You're dealing with an object; you can't just write over a value in the an array (as you were attempting in the example).

Load the object, then use a method to get or set it. For example, if I were looking at a custom field on a user object, the following code would: 1. Load the object 2. Fetch the specific custom field 3. Update the specific custom field. 4. Reload the value (just to make sure it is what I think it should be)

//  Load user object (UID is the userid I'm interested in)
$userProfile = \Drupal\user\Entity\User::load($uid);

// Get field data from that user.
$firstname = $userProfile->get('field_user_first_name')->value;
$userProfile->set('field_user_first_name', 'Newname');
$firstname = $userProfile->get('field_user_first_name')->value;

'field_user_first_name' is the machine name I assigned when I created the custom field

enter image description here

  • 1
    This is not an answer to the question, hook_ENTITY_TYPE_view is about the render array passed by reference in $build, not about an user object. – 4k4 Feb 6 at 10:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.