This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Isaicp 5330: Ensure editable non-federated fields. (#1684)
Isaicp 5330: Ensure editable non-federated fields.
- Loading branch information
Showing
9 changed files
with
235 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...m/joinup_federation/src/Plugin/Validation/Constraint/NotNullUnlessFederatedConstraint.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Drupal\joinup_federation\Plugin\Validation\Constraint; | ||
|
||
use Symfony\Component\Validator\Constraints\NotNull; | ||
|
||
/** | ||
* NotNull constraint. | ||
* | ||
* Overrides the Drupal Core validation to handle federated entities. | ||
*/ | ||
class NotNullUnlessFederatedConstraint extends NotNull {} |
87 changes: 87 additions & 0 deletions
87
...federation/src/Plugin/Validation/Constraint/NotNullUnlessFederatedConstraintValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Drupal\joinup_federation\Plugin\Validation\Constraint; | ||
|
||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | ||
use Drupal\Core\TypedData\ListInterface; | ||
use Drupal\Core\TypedData\Validation\TypedDataAwareValidatorTrait; | ||
use Drupal\Core\Validation\Plugin\Validation\Constraint\NotNullConstraintValidator; | ||
use Drupal\rdf_entity\RdfInterface; | ||
use Drupal\rdf_entity_provenance\ProvenanceHelperInterface; | ||
use Drupal\rdf_schema_field_validation\SchemaFieldValidatorInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* NotNull constraint validator. | ||
* | ||
* Overrides the Drupal NotNull validator to allow empty when it refers to a | ||
* field that belongs to a federated entity. | ||
* | ||
* Used for fields that have taxonomy references that are not provided into, or | ||
* is not mapped in the federated record. | ||
*/ | ||
class NotNullUnlessFederatedConstraintValidator extends NotNullConstraintValidator implements ContainerInjectionInterface { | ||
|
||
use TypedDataAwareValidatorTrait; | ||
|
||
/** | ||
* The provenance helper service. | ||
* | ||
* @var \Drupal\rdf_entity_provenance\ProvenanceHelperInterface | ||
*/ | ||
protected $provenanceHelper; | ||
|
||
/** | ||
* The field validator service. | ||
* | ||
* @var \Drupal\rdf_schema_field_validation\SchemaFieldValidatorInterface | ||
*/ | ||
protected $fieldValidator; | ||
|
||
/** | ||
* Creates a new validator. | ||
* | ||
* @param \Drupal\rdf_entity_provenance\ProvenanceHelperInterface $provenance_helper | ||
* The provenance helper service. | ||
* @param \Drupal\rdf_schema_field_validation\SchemaFieldValidatorInterface $field_validator | ||
* The field validator service. | ||
*/ | ||
public function __construct(ProvenanceHelperInterface $provenance_helper, SchemaFieldValidatorInterface $field_validator) { | ||
$this->provenanceHelper = $provenance_helper; | ||
$this->fieldValidator = $field_validator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static( | ||
$container->get('rdf_entity_provenance.provenance_helper'), | ||
$container->get('rdf_schema_field_validation.schema_field_validator') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate($value, Constraint $constraint) { | ||
$typed_data = $this->getTypedData(); | ||
if ($typed_data instanceof ListInterface) { | ||
$parent_entity = $value->getParent()->getEntity(); | ||
// Check for the parent entity being an rdf entity so that we quickly skip | ||
// other entity types that use the NotNull constraint. | ||
if (($parent_entity instanceof RdfInterface) && !empty($parent_entity->id())) { | ||
$entity_type_id = $parent_entity->getEntityTypeId(); | ||
$bundle = $parent_entity->bundle(); | ||
if ($this->fieldValidator->hasSchemaDefinition($entity_type_id, $bundle) | ||
&& $this->fieldValidator->isDefinedInSchema($entity_type_id, $bundle, $typed_data->getName()) | ||
&& $this->provenanceHelper->loadProvenanceActivity($parent_entity->id())) { | ||
return; | ||
} | ||
} | ||
} | ||
parent::validate($value, $constraint); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters