You are here

public static function LayoutBuilderIdsConfigureBlock::layoutBuilderIdsConfigureBlockFormValidation in Layout builder ids 2.0.x

File

src/EventSubscriber/LayoutBuilderIdsConfigureBlock.php, line 80

Class

LayoutBuilderIdsConfigureBlock
Add block ids to layout builder sections.

Namespace

Drupal\layout_builder_ids\EventSubscriber

Code

public static function layoutBuilderIdsConfigureBlockFormValidation(array &$form, FormStateInterface $form_state) {

  // Get the layout builder id from the form,
  // also put the id through the HTML getId to make sure
  // that we form a valid id.
  $layout_builder_id = Html::getId($form_state
    ->getValue([
    'settings',
    'layout_builder_id',
  ]));

  // Ensure that we have an actual id to check.
  if ($layout_builder_id !== '' && $layout_builder_id !== NULL) {

    // Check if we have a duplicate id somewhere.
    $found_id = LayoutBuilderIdsService::layoutBuilderIdsCheckIds($layout_builder_id, $form_state, 'block');

    // If we have a duplicate id, then set the form error.
    if ($found_id) {

      // Set the form error on the layout builder id element.
      $form_state
        ->setError($form['settings']['layout_builder_id'], 'There is already a block or section with the ID "' . $layout_builder_id . '".');
    }
  }
}