-3
function test_node_view(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  updateDisplay();
}

function updateDisplay() {
  unset(['field_test']); //I want to Unset this field
}
  • updateDisplay() doesn't get any argument, nor does it define any parameter. unset(['field_test']) contains a syntax error (PHP Parse error: syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM)). The render array is contained in $build; everything that hook implementation should alter is there. It is then preferable to avoid the logged-in user has access to the field, instead of removing it from the render array. – kiamlaluno Jul 10 at 11:02
  • Questions cannot be edited, once they are answered, if the edit invalidates the given answers. Questions aren't thought as back-and-forth between the user who asks the question and the users who answer, so they should not be changed basing on the given answers, and the users who answered should not be expected to change their answers basing on the question edit, which was triggered by a given answer. – kiamlaluno Jul 11 at 8:46
  • Furthermore, asking a question doesn't mean to simply put a title (which doesn't count as explicit question) and showing the code in the question body. There are more details to add. As it is, the question seems showing code that has not even be tried. – kiamlaluno Jul 11 at 8:50
1

It's hard to tell exactly what you're going for, but to elaborate on @kiamlaluno's comment, you'd want to do something like the following.

First, update your updateDisplay() function to accept your $node's $build array as a parameter passed by reference. This allows you to make alterations to the variable inside your function that will be preserved outside your function.

// Note the ampersand prepended to the variable's name.
function updateDisplay(&$node_build) {
  // ... code ...
}

Second, it's preferable to remove access to the field rather than unset it from the array. This will prevent it from being rendered. You can do this by setting the field's #access value to false. Your updated code might look like:

function updateDisplay(&$node_build) {
  // Remove access to field_test
  $node_build['field_test']['#access'] = FALSE;
}

Lastly, you'll have to update your hook_node_view() implementation to pass the build array to your updateDisplay() function.

function test_node_view(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  updateDisplay($build);
}
| improve this answer | |
  • 1
    If the code just needs to hide a single render element, I would rather avoid to call a function for that. – kiamlaluno Jul 11 at 8:52
  • 1
    Oh, absolutely. I'm guessing there's more to it, some logic, etc, or I would expect the field to be removed on the Manage Display screen rather than in code like this. – sonfd Jul 11 at 14:08
  • I hope there is more code than what the question shows, or that OP reads your comment. If all the code the OP is using is just a unset(), hiding the field from the Manage Display page is the correct way to hide it. – kiamlaluno Jul 11 at 17:54

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