You are here

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

Function to check the sections for duplicate ids.

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::layoutBuilderIdsCheckSectionIds

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

File

src/Service/LayoutBuilderIdsService.php, line 30

Class

LayoutBuilderIdsService
Class UWService.

Namespace

Drupal\layout_builder_ids\Service

Code

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

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

  // Set the delta to null to start.
  $delta = NULL;

  // If we are on a section check, then get the delta
  // form the form state.
  if ($type == 'section') {

    // Get the delta from the form state.
    $delta = $form_state
      ->getBuildInfo()['args'][1];
  }

  // Step through each section and check for duplicate id.
  foreach ($sections as $index => $section) {

    // If we are on a section check and the delta is the same
    // as the index we are on, just skip over the check, so
    // that we are not checking the current section.
    if ($type == 'section' && $delta == $index) {
      continue;
    }

    // Get the layout settings for the section.
    $layout_settings = $section
      ->getLayoutSettings();

    // If there is a layout_builder_id setting and it is the same as the
    // specified id, then return TRUE, as we found a duplicate id.
    if (isset($layout_settings['layout_builder_id']) && $layout_settings['layout_builder_id'] == $layout_builder_id) {
      return TRUE;
    }
  }

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