Skip to content

Instantly share code, notes, and snippets.

@EclipseGc
Created December 30, 2013 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EclipseGc/8185148 to your computer and use it in GitHub Desktop.
Save EclipseGc/8185148 to your computer and use it in GitHub Desktop.
<?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;
}
<?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