- πͺπΈSpain guardiola86
I tried using Block Class module and didn't work for me. My issue was that I couldn't use the same validate and submit functions that were working in the block edit page, but not when using layout builder. I ended up using something like this:
/** * Implements hook_entity_insert(). */ function embeddocument_block_entity_insert(EntityInterface $entity) { _embeddocument_block_entity_insert_or_update($entity); } /** * Implements hook_entity_update(). */ function embeddocument_block_entity_update(EntityInterface $entity) { _embeddocument_block_entity_insert_or_update($entity); } /** * Custom callback used by insert and update. * * @param $entity * @throws \Drupal\Core\Entity\EntityStorageException */ function _embeddocument_block_entity_insert_or_update($entity) { if (\Drupal::moduleHandler()->moduleExists('block_content')) { $entity_langcode = $entity->get('langcode')->value; if ($entity->bundle() == 'document') { $entity_array = $entity->toArray(); if (isset($entity_array['field_documents'])) { $mid = $entity_array['field_documents'][0]['target_id']; if (!empty($mid) && !empty($entity_langcode)) { $media = Media::load($mid); // If translation doesn't exist, create it. if (!$media->hasTranslation($entity_langcode)) { $media->addTranslation($entity_langcode, $media->toArray()); } $media->save(); } } } } }