3

i must render entities but it render the node title / author / creation_date etc... and i want to show only fields of my node. In D7 we can set a #access property to not render the field but in D8, i don't know how to do this. Can you help me please ?

I render my entities by this way :

// Get all content observable
            $query = \Drupal::entityQuery('node');
            $query->condition('status', 1);
            $query->condition('type', $ctype_observable);
            $query->condition('field_scm_tag', $this->getPluginDefinition()['tag']['machine_name']);
            $entity_ids = $query->execute();
            $entities = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($entity_ids);

            // render the entity
            $view_mode = 'default';
            $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
            foreach ($entities as $nid => $entity) {
                $build = $view_builder->view($entity, $view_mode);
                //kint($build['#node']);
                dsmr($build['#node']->get('title'));
                $build['content'][$nid]['#markup'] = render($build);
            }
  • Have you tried $build['#node']['#title'] = '';? I've never done it with a node before, but when I want to hide title in a custom block I'm rendering I do it like so: pastie.org/private/7c5deyt4k69kic2rqoxxla – Matt Dec 5 '16 at 16:17
  • Yes i can use this : $build['#node']->setTitle(''); But when i used D7 i always use the #access like $block['item']['#access']=FALSE; to not render the fields, it permit to not erase the information. Then i search a good way to make it properly. But i have this "submitted by admin the mon 05/12/2016 - 16:36" to hide – Kpone Dec 5 '16 at 16:36
2

This doesn't work anymore like this and the reason is render caching.

After calling view(), you only get a minimal render array, that will use #pre_render callbacks to complete itself in case of a cache miss. On a cache hit, it will return the cached HTML, without having to build all the fields.

The information you want to hide is in the template, both the title and the authored by information (which can be hidden for a node type for all bundles but not per view-mode).

The correct way to deal with that is to define your own view mode (default is wrong anyway, you should use 'full', not 'default', default is the fallback view display, not a view mode).

For that view mode, you can then provide a template/preprocess hook/hook_entity_view/hook_entity_view_alter hook, use any of those or a combination of it to achieve the desired result. To not show the authored by information, you can for example unset $variables['display_submitted'] as it uses that in the default node.html.twig template. Or provide a template suggestion for your view mode that removes that part completely. Both work, a preprocess is a bit easier from a module.

The important part is that you need your own view mode for that unless it is ok that full should never show that information, then that's fine too. The default cache key is entity type/entity id/view mode. There are other ways, you could add a cache context or you could even add another cache key, but you still need to do it within those hooks/template and the most obvious way to flag it for that is a view mode, that's what they exist for.

|improve this answer|||||
  • Thanks, it's a good way to use templating to render nodes. I will do this ;) – Kpone Dec 6 '16 at 9:23

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.