Commit d5ffb572 authored by subhojit777's avatar subhojit777 Committed by bojanz
Browse files

Issue #2586275 by subhojit777: Disallow creating a zone with a...

Issue #2586275 by subhojit777: Disallow creating a zone with a zone_member_zone that references the parent zone
parent 442aa8a0
...@@ -205,12 +205,9 @@ class ZoneForm extends EntityForm { ...@@ -205,12 +205,9 @@ class ZoneForm extends EntityForm {
*/ */
public function addMemberSubmit(array $form, FormStateInterface $form_state) { public function addMemberSubmit(array $form, FormStateInterface $form_state) {
$plugin_id = $form_state->getValue('plugin'); $plugin_id = $form_state->getValue('plugin');
$weight = count($this->entity->getMembers());
/** @var \Drupal\address\Plugin\ZoneMember\ZoneMemberInterface $member */ /** @var \Drupal\address\Plugin\ZoneMember\ZoneMemberInterface $member */
$member = $this->memberManager->createInstance($plugin_id); $member = $this->memberManager->createInstance($plugin_id, ['weight' => $weight], $this->entity);
// @todo Clean up once zone members get a setWeight() method.
$configuration = $member->getConfiguration();
$configuration['weight'] = count($this->entity->getMembers());
$member->setConfiguration($configuration);
$this->entity->addMember($member); $this->entity->addMember($member);
$form_state->setRebuild(); $form_state->setRebuild();
} }
......
<?php
namespace Drupal\address\Plugin\EntityReferenceSelection;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
/**
* Provides en entity reference selection plugin for zones.
*
* @EntityReferenceSelection(
* id = "default:zone",
* label = @Translation("Zone selection"),
* entity_types = {"zone"},
* group = "default",
* weight = 1
* )
*/
class ZoneSelection extends DefaultSelection {
/**
* {@inheritdoc}
*/
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityQuery($match, $match_operator);
// The 'zone' zone member needs to be able to exclude the parent zone
// from selection. It does this by passing a custom skip_id parameter
// to the entity_autocomplete form element via #handler_settings.
$handler_settings = $this->configuration['handler_settings'];
if (!empty($handler_settings['skip_id'])) {
$query->condition('id', $handler_settings['skip_id'], '<>');
}
return $query;
}
}
...@@ -76,6 +76,13 @@ class ZoneMemberZone extends ZoneMemberBase implements ContainerFactoryPluginInt ...@@ -76,6 +76,13 @@ class ZoneMemberZone extends ZoneMemberBase implements ContainerFactoryPluginInt
'#target_type' => 'zone', '#target_type' => 'zone',
'#tags' => FALSE, '#tags' => FALSE,
'#required' => TRUE, '#required' => TRUE,
'#selection_settings' => [
'skip_id' => $this->parentZone->id(),
'sort' => [
'field' => 'name',
'direction' => 'ASC',
],
],
]; ];
return $form; return $form;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment