2

I have seen lots of answers for getting when a node was last edited, but I can't seem to apply any of these methods to blocks. Basically, I have a block that I only want to display if it has been updated in the last week. When I view blocks in the admin, I see that the admin view shows you when a block was last edited.

I can get my block in hook_preprocess_block, and get things like $block->label(), but any variation of $block->getChangedTime(); or $block->changed; fails for me.

And really, I need this value in my html template, not the block. So I need to get the specific block by ID, in hook_preprocess_html, get the time it was last updated as a unix timestamp, and this would give me the value I need for my twig template.

2 Answers 2

2

This is a method of content entities implementing the EntityChangedInterface, like for example node and block_content. In a block template, which is a template of the configured block, you find it in the content array:

if (isset($variables['elements']['content']['#block_content'])) {
  $changed_time = $variables['elements']['content']['#block_content']->getChangedTime();
}

If you want to load the block content entity outside of the block template, see How to get a field value of custom block?

1

$block->updated should do the trick.

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.