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.

Issue fork block_id-3261612

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

perfectcu.be created an issue. See original summary.

heni_deepak’s picture

@perfectcu.be
Thank you for your patch. I will plan to release the next beta version soon.

heni_deepak’s picture

StatusFileSize
new1.2 KB

reload the patch.

heni_deepak’s picture

Status: Active » Needs work
heni_deepak’s picture

Status: Needs work » Needs review
radheymkumar’s picture

#5 works for me.

  • heni_deepak committed e858a53 on 2.0.x
    Issue #3261612 by heni_deepak, perfectcu.be: Existing blocks cannot be...
heni_deepak’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

radheymkumar’s picture

Version: 2.0.0-beta1 » 2.0.2
Assigned: Unassigned » radheymkumar
Status: Closed (fixed) » Fixed

Thanks

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.