You are here

public static function LayoutBuilderIdsService::layoutBuilderIdsCheckBlockIds in Layout builder ids 2.0.x

A function to check the blocks for a duplicate id.

Parameters

string $layout_builder_id: A string representing the if we are looking for.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

string $type: A string representing the type of check, either section or block.

Return value

bool A boolean value to whether or not there is a duplicate id.

Overrides LayoutBuilderIdsServiceInterface::layoutBuilderIdsCheckBlockIds

1 call to LayoutBuilderIdsService::layoutBuilderIdsCheckBlockIds()
LayoutBuilderIdsService::layoutBuilderIdsCheckIds in src/Service/LayoutBuilderIdsService.php
Function to check for duplicate ids.

File

src/Service/LayoutBuilderIdsService.php, line 77

Class

LayoutBuilderIdsService
Class UWService.

Namespace

Drupal\layout_builder_ids\Service

Code

public static function layoutBuilderIdsCheckBlockIds(string $layout_builder_id, FormStateInterface $form_state, string $type) : bool {

  // Get the sections from the formObject.
  $sections = $form_state
    ->getFormObject()
    ->getSectionStorage()
    ->getSections();

  // If we are on a block check, get the current component uuid.
  if ($type == 'block') {

    // Get the current component uuid.
    $current_component_uuid = $form_state
      ->getFormObject()
      ->getCurrentComponent()
      ->get('uuid');
  }

  // Step through each section and get the blocks to
  // check for duplicate ids.
  foreach ($sections as $section) {

    // Get the components of the section.
    $components = $section
      ->getComponents();

    // Step through each of the components and check for duplicate ids.
    foreach ($components as $uuid => $component) {

      // If we are on a block check and we are looking at the current
      // component, skip the check for ID.
      if ($type == 'block' && $uuid == $current_component_uuid) {
        continue;
      }

      // Get the additional setting from the component.
      $additional = $component
        ->get('additional');

      // Ensure that the layout_builder_id is set in additional settings.
      if (isset($additional['layout_builder_id'])) {

        // If there is already an id with the one specified return TRUE.
        if ($additional['layout_builder_id'] == $layout_builder_id) {
          return TRUE;
        }
      }
    }
  }

  // Return FALSE as we will return TRUE if we find a duplicate.
  return FALSE;
}