1

I'm a little bit confused with hide() documentation. It says:

The first time render() or drupal_render() is called on an element tree, as each element in the tree is rendered, it is marked with a "#printed" flag and the rendered children of the element are cached. Subsequent calls to render() or drupal_render() will not traverse the child tree of this element again: they will just use the cached children. So if you want to hide an element, be sure to call hide() on the element before its parent tree is rendered for the first time, as it will have no effect on subsequent renderings of the parent tree.

First of all, I want to say I understand that hide() allow to avoid rendering of a specific element.

But documentation says more :

  1. By talking about caching element as it is the default behaviour. For me, if #cache property is not defined for an element, when rendering the element will be not be cached.
  2. And for me also, subsequent calls to drupal_render() will not use the cached element, because #printed property will be checked before trying to get the cached element (#printed=TRUE means return '';) .

I want to get your explanation about this.

1 Answer 1

1

Render arrays are trees. If you hide() the subtree P1.P2.P3.P4, it's stil possible for a supertree (P1.P2 or P1.P2.P3) to have caching enabled (i.e. have #cache set).

Also note that just having #cache set does not mean that the subtree with that element as a root does not get cached per se. Only when #cache[keys] is set is render caching triggered. #cache can also just contain cacheability metadata (tags, contexts, max-age) describing the cacheability of that subtree.

See https://www.drupal.org/developing/api/8/render/arrays/cacheability.

3
  • Thank, but I still use Drupal 7.. On another hand, i don't understand your answer,which doesn't answer really my 2 points. For example hide() documentation seems to say that caching is the normal (default) behaviour when render an element tree. Try to read hide() documentation for Drupal 7.
    – ostry.sn
    Commented Feb 24, 2016 at 22:33
  • Oh, Drupal 7. Pretty much nothing in Drupal 7 uses render caching. It may not be worth bothering.
    – Wim Leers
    Commented Feb 25, 2016 at 19:40
  • Note that the explanation I gave is for the overall algorithm/render pipeline and applies to D7 too.
    – Wim Leers
    Commented Feb 25, 2016 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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