1

I want to update a field on a Custom Block Type in an update hook. I've seen examples updating fields on nodes, but it appears this doesn't work on block fields, which is surprising.

function ra_homepage_update_8004() {
$form = "some string.";
$uuid = 'f8b451ff-8bt5-494e-be81-sdfaasdfsadf';
if($block = \Drupal::service('entity.repository')->loadEntityByUuid('block_content', $uuid)) {

$block->set('field_form', $form);

}
}

The real question is how do I view the available methods on the field, because I've tried $block->get('field_form')->set($form); as well as $block->get(0)->setValue($form); and probably a few other things, but it's all just shots in the dark.

5
  • D8 right? Which hook are you using? – No Sssweat Feb 9 '17 at 7:50
  • ->set('field_name', 'new value') is the most basic method to set a field. If this doesn't work there is somewhere else a problem. Perhaps wrong field name or another block content type loaded? Did you check the error log? – 4k4 Feb 9 '17 at 13:25
  • For starters, I don't see where you actually save the block? what doesn't work, do you get an error? If not, then it might be as simple as the missing save call – Berdir Feb 9 '17 at 18:47
  • yes D8, hook_update_N is the hook. – icicleking Feb 9 '17 at 21:28
  • missing ->save() call might be it face-palm. I ended up just making a new block with the same value fields, which does get ->save()d. If I have a chance I'll test tomorrow. – icicleking Feb 9 '17 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.