Existing blocks cannot be edited without changing their ID; error: "Attribute ID must be unique. This ID has added in another block."

Created on 31 January 2022, almost 3 years ago
Updated 12 March 2024, 8 months ago

Problem/Motivation

Existing blocks, with a block_id.module ID attached, cannot be edited without changing the already set ID.

Steps to reproduce

  1. edit any block, add an id to it, save block.
  2. re-edit the same block, save form without changing previously set id, edit form is re-shown with error "Attribute ID must be unique. This ID has added in another block."

Proposed resolution

Remove the current block from the list of external blocks checked in block_id/block_id.module @ W3S_validation() @ variable $block_id; patch attached .

After applying patch, new variable $self:

/**
 * Implement w3 standards validation alter.
*/
function W3S_validation(&$form, FormStateInterface $form_state){
  // Block ID field value fatch.
  $inputs = $form_state->getUserInput()['third_party_settings']['block_id']['id'];
  // get the id of the block being submitted
  $self = isset($form['id']['#default_value']) ? $form['id']['#default_value'] : null;
  if(!empty($inputs)){
    // Checking id has not no space and special characters.
    if ( preg_match('/[^a-zA-Z_\-0-9]/i', $inputs) ){
      $form_state->setErrorByName('third_party_settings', t('Attribute ID must be unique, must not contain any space characters & must contain at least one character. Underscore (_) can be used.'));
    }
    // block list those using block id field.
    $block_ids = \Drupal::entityQuery('block')->condition('third_party_settings', '')->execute();
    // remove the block currently being submitted from the list of existing blocks so the submitted ID isn't checked as an external block ID
    if ($self) { unset($block_ids[$self]); }
    // Getting ID's already inserted in another block.
    foreach($block_ids as $ids){
      $block = Block::load($ids);
      if($block->getThirdPartySetting('block_id', 'id') == $inputs){
        $form_state->setErrorByName('third_party_settings', t('Attribute ID must be unique. This ID has added in another block.'));
      }
    }
  }
}

Remaining tasks

Review attached patch , roll it into the next release?

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇺🇸United States perfectcu.be

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024