Created
December 30, 2013 17:33
-
-
Save EclipseGc/8185148 to your computer and use it in GitHub Desktop.
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
<?php | |
// Adding a method to TypedDataManager | |
public function mapDefinition(array $type) { | |
/** $type comes across as: | |
* | |
* "type" = "entity", | |
* "constraints" = { | |
* "EntityType" = "node" | |
* } | |
* | |
*/ | |
$definition = new DataDefinition($this->getDefinition($type['type'])); | |
foreach ($type['constraints'] as $constraint_type => $value) { | |
$definition->addConstraint($constraint_type, $value); | |
} | |
return $definition; | |
} |
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
<?php | |
// Altering addConstraint within DataDefinition & co to something like so: | |
public function addConstraint($constraint_name, $options = NULL) { | |
$constraint_definition = $this->constraintManager->getDefinition($constraint_name); | |
if (!isset($this->definition['constraints'][$constraint_name])) { | |
$this->definition['constraints'][$constraint_name] = $options; | |
} | |
elseif ($constraint_definition->supportMultiple()) { | |
$this->definition['constraints'][$constraint_name] = $options; | |
} | |
else { | |
throw new ConstraintException("Nuh uh"); | |
} | |
// This constraint may need to alter the data type. | |
$new_type = $constraint_definition->alterDataType($this->getDataType()); | |
if ($new_type != $this->getDataType()) { | |
$type_data_definition = \Drupal::service('typed_data')->getDefinition($new_type); | |
// This will add the constraints during construction. | |
$definition = new DataDefinition($typed_data_definition + $this->definition); | |
$definition->setDataType($new_type); | |
return $definition; | |
} | |
return $this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment