Problem/Motivation
Existing blocks, with a block_id.module ID attached, cannot be edited without changing the already set ID.
Steps to reproduce
- edit any block, add an id to it, save block.
- 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.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 3261612-4.patch | 1.2 KB | heni_deepak |
| block_id-2.0.0-beta1--dont-check-current-block-id-as-external-block-id.patch | 1.31 KB | perfectcu.be |
Issue fork block_id-3261612
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:
- 2.0.x
changes, plain diff MR !5
- 3261612-existing-blocks-cannot
changes, plain diff MR !2
Comments
Comment #2
heni_deepak commented@perfectcu.be
Thank you for your patch. I will plan to release the next beta version soon.
Comment #5
heni_deepak commentedreload the patch.
Comment #6
heni_deepak commentedComment #7
heni_deepak commentedComment #8
radheymkumar commented#5 works for me.
Comment #11
heni_deepak commentedComment #13
radheymkumar commentedThanks