Problem/Motivation

This is a follow-up issue from #2999491: Add reusable option to inline block creation, specifically #2999491-88: Add reusable option to inline block creation (as requested in #2999491-97: Add reusable option to inline block creation):

...It might be a good idea to have a follow-up issue to decide how to choose when adding a new block.

Should we add an option to fork a reusable block to a non-reusable inline-block (and optionally delete the reusable block IF it's not used elsewhere?

Steps to reproduce

@tbd

Proposed resolution

@tbd

Remaining tasks

@tbd

User interface changes

@tbd

API changes

@tbd

Data model changes

@tbd

Release notes snippet

@tbd

CommentFileSizeAuthor
#4 fork-blocks.png43.96 KBluke.leber

Comments

Webbeh created an issue. See original summary.

Webbeh’s picture

Issue summary: View changes

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

luke.leber’s picture

StatusFileSize
new43.96 KB

Huge +1, but I think that adding a delete option may slow things down a bit.

Very rough attempt at something like this! Probably riddled with gotchas and short-sightedness, but I wanted to continue to conversation!

New option to copy a reusable block to an inline block instance on the block configure form

<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;

/**
 * Implements hook_form_alter().
 */
function lb_block_fork_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id === 'layout_builder_add_block' || $form_id === 'layout_builder_update_block') {
    /** @var \Drupal\layout_builder\Form\ConfigureBlockFormBase $configure_form */
    $configure_form = $form_state->getFormObject();
    $component = $configure_form->getCurrentComponent();

    $config = $component->get('configuration');
    if (isset($config['id']) && str_starts_with($config['id'], 'block_content:')) {
      $form['#prefix'] = '<div id="fork-block">';
      $form['#suffix'] = '</div>';
      $form['settings']['fork'] = [
        '#type' => 'container',
      ];

      $form['settings']['fork']['notice'] = [
        '#type' => 'html_tag',
        '#tag' => 'strong',
        '#value' => new TranslatableMarkup('This is a reusable block, create inline copy?'),
      ];

      $form['settings']['fork']['action'] = [
        '#type' => 'button',
        '#value' => new TranslatableMarkup('Copy'),
        '#ajax' => [
          'callback' => '_lb_block_fork_callback',
          'wrapper' => 'fork-block',
        ],
      ];
    }
  }
}

function _lb_block_fork_callback(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\layout_builder\Form\ConfigureBlockFormBase $configure_form */
  $configure_form = $form_state->getFormObject();
  $section_storage = $configure_form->getSectionStorage();
  $section = $configure_form->getCurrentSection();
  $component = $configure_form->getCurrentComponent();
  $config = $component->get('configuration');

  $parts = explode(':', $config['id']);

  /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
  $entity_repository = \Drupal::service('entity.repository');

  /** @var \Drupal\block_content\BlockContentInterface $block */
  $block = $entity_repository->loadEntityByUuid('block_content', $parts[1]);
  if ($block) {

    $clone = $block->createDuplicate();
    $clone->setNonReusable();

    $component->setConfiguration([
      'id' => "inline_block:{$block->bundle()}",
      'label' => $form_state->getValue(['settings', 'label']),
      'label_display' => $form_state->getValue(['settings', 'label_display']),
      'provider' => 'layout_builder',
      'view_mode' => 'full',
      'context_mapping' => [],
      'block_revision_id' => $block->getRevisionId(),
      'block_serialized' => NULL,
    ]);

    /** @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $tempstore */
    $tempstore = \Drupal::service('layout_builder.tempstore_repository');
    $tempstore->set($section_storage);

    $form_state->setRedirectUrl(Url::fromRoute('layout_builder.update_block', [
      'section_storage_type' => $section_storage->getStorageType(),
      'section_storage' => $section_storage->getStorageId(),
      'delta' => array_search($section, $section_storage->getSections()),
      'region' => $component->getRegion(),
      'uuid' => $component->getUuid(),
    ]));
  }

  return $form;
}

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.