Score:2

Get the time a custom block was last edited

in flag
3x5

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.

Score:2
cn flag

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?

Score:1
de flag

$block->updated should do the trick.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.