Skip to main content
Added more info about latest attempt
Source Link
Amy
  • 5.8k
  • 13
  • 66
  • 133

UPDATE 4: I'm now attempting to duplicate the blocks but I can't seem to load the blocks from the variant config.

$conf = $var_clone->getVariantPlugin()->getConfiguration();
$blocks_config = $conf['blocks'];
foreach ($blocks_config as $uuid => $block_data) {
    var_dump($uuid);
    var_dump($block_data);
}

Gives me the UUID but I want to actually have an object instead of an array so I can do

$block_clone = $block->createDuplicateBlock();

But I can't seem to get the block object from the UUID. I've even tried loading by the Uuid specifying every type of entity possible like so.

$conf = $var_clone->getVariantPlugin()->getConfiguration();
$blocks_config = $conf['blocks'];
foreach ($blocks_config as $uuid => $bdata) {
                foreach (array_keys(\Drupal::entityTypeManager()->getDefinitions()) as $etype) {
                    $block = \Drupal::service('entity.repository')->loadEntityByUuid($etype, $uuid);
                    var_dump($block);
                }
}

But I get nothing but NULL when trying to print each attempt at loading the entity by the UUID I got from the variant block configuration. Are config entities loaded a different way? Or does the UUID supplied not refer to something that can be loaded?


UPDATE 4: I'm now attempting to duplicate the blocks but I can't seem to load the blocks from the variant config.

$conf = $var_clone->getVariantPlugin()->getConfiguration();
$blocks_config = $conf['blocks'];
foreach ($blocks_config as $uuid => $block_data) {
    var_dump($uuid);
    var_dump($block_data);
}

Gives me the UUID but I want to actually have an object instead of an array so I can do

$block_clone = $block->createDuplicateBlock();

But I can't seem to get the block object from the UUID. I've even tried loading by the Uuid specifying every type of entity possible like so.

$conf = $var_clone->getVariantPlugin()->getConfiguration();
$blocks_config = $conf['blocks'];
foreach ($blocks_config as $uuid => $bdata) {
                foreach (array_keys(\Drupal::entityTypeManager()->getDefinitions()) as $etype) {
                    $block = \Drupal::service('entity.repository')->loadEntityByUuid($etype, $uuid);
                    var_dump($block);
                }
}

But I get nothing but NULL when trying to print each attempt at loading the entity by the UUID I got from the variant block configuration. Are config entities loaded a different way? Or does the UUID supplied not refer to something that can be loaded?

added 986 characters in body
Source Link
Amy
  • 5.8k
  • 13
  • 66
  • 133

UPDATE 3: I managed to get the variant to show up by setting the id of the variant clone like so

        $hash = bin2hex(random_bytes(8));
        $var_clone = $variant->createDuplicate();
        $var_clone->set('page', $clone->id());
        $var_clone->setPageEntity($clone);
        $var_clone->set('id', $hash);
        $var_clone->save();

It seems like it wasn't saving because an id wasn't set for the variant. However now there are new errors that I receive whenever trying to edit or save the page variant.

Drupal\Core\Config\ConfigDuplicateUUIDException: Attempt to save a configuration entity 'test' with UUID '5d091883-6667-4aab-9fd8-c8ef77c871f4' when this entity already exists with UUID '11066a36-ca97-428f-aa78-f17b034421d8' in Drupal\Core\Config\Entity\ConfigEntityBase->preSave() (line 339 of /mnt/www/html/mysite/docroot/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php).


UPDATE 3: I managed to get the variant to show up by setting the id of the variant clone like so

        $hash = bin2hex(random_bytes(8));
        $var_clone = $variant->createDuplicate();
        $var_clone->set('page', $clone->id());
        $var_clone->setPageEntity($clone);
        $var_clone->set('id', $hash);
        $var_clone->save();

It seems like it wasn't saving because an id wasn't set for the variant. However now there are new errors that I receive whenever trying to edit or save the page variant.

Drupal\Core\Config\ConfigDuplicateUUIDException: Attempt to save a configuration entity 'test' with UUID '5d091883-6667-4aab-9fd8-c8ef77c871f4' when this entity already exists with UUID '11066a36-ca97-428f-aa78-f17b034421d8' in Drupal\Core\Config\Entity\ConfigEntityBase->preSave() (line 339 of /mnt/www/html/mysite/docroot/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php).

added 281 characters in body
Source Link
Amy
  • 5.8k
  • 13
  • 66
  • 133

I tried doubling up and trying multiple methods like so

        $var_clone = $variant->createDuplicate();
        $var_clone->set('page', $clone->id());
        $var_clone->setPageEntity($clone);
        $var_clone->save();

but that also had no effect.

I tried doubling up and trying multiple methods like so

        $var_clone = $variant->createDuplicate();
        $var_clone->set('page', $clone->id());
        $var_clone->setPageEntity($clone);
        $var_clone->save();

but that also had no effect.

added 928 characters in body
Source Link
Amy
  • 5.8k
  • 13
  • 66
  • 133
Loading
added 123 characters in body
Source Link
Amy
  • 5.8k
  • 13
  • 66
  • 133
Loading
Source Link
Amy
  • 5.8k
  • 13
  • 66
  • 133
Loading