Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
     $constraints = parent::getConstraints();
     $constraints[] = $constraint_manager->create('ComplexData', array('value' => array('TestField' => array('value' => -1, 'message' => t('%name does not accept the value @value.', array('%name' => $this->getFieldDefinition()->getLabel(), '@value' => -1))))));
     return $constraints;
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
     $constraints = parent::getConstraints();
     $constraints[] = $constraint_manager->create('ComplexData', array('value' => array('Regex' => array('pattern' => '/^[+-]?((\\d+(\\.\\d*)?)|(\\.\\d+))$/i'))));
     return $constraints;
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
     $constraints = parent::getConstraints();
     $constraints[] = $constraint_manager->create('ComplexData', array('value' => array('Length' => array('max' => Email::EMAIL_MAX_LENGTH, 'maxMessage' => t('%name: the email address can not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => Email::EMAIL_MAX_LENGTH))))));
     return $constraints;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $manager = \Drupal::typedDataManager()->getValidationConstraintManager();
     $constraints = parent::getConstraints();
     $constraints[] = $manager->create('ComplexData', ['amount' => ['Regex' => ['pattern' => '/^[+-]?((\\d+(\\.\\d*)?)|(\\.\\d+))$/i']]]);
     return $constraints;
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraints = parent::getConstraints();
     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
     $constraints[] = $constraint_manager->create('ComplexData', array('name' => array('Length' => array('max' => 64, 'maxMessage' => t('%name: may not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => 64))))));
     return $constraints;
 }
Esempio n. 6
0
 /**
  * Gets the typed data manager.
  *
  * @return \Drupal\Core\TypedData\TypedDataManagerInterface
  *   The typed data manager.
  */
 public function getTypedDataManager()
 {
     if (empty($this->typedDataManager)) {
         $this->typedDataManager = \Drupal::typedDataManager();
     }
     return $this->typedDataManager;
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
     $constraints = parent::getConstraints();
     $constraints[] = $constraint_manager->create('ComplexData', array('value' => array('Length' => array('max' => static::COUNTRY_ISO2_MAXLENGTH, 'maxMessage' => t('%name: the country iso-2 code may not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => static::COUNTRY_ISO2_MAXLENGTH))))));
     return $constraints;
 }
Esempio n. 8
0
 /**
  * {@inheritdoc}
  */
 public function setValue($value, $notify = TRUE)
 {
     $this->target = \Drupal::typedDataManager()->create($this->definition->getTargetDefinition(), $value);
     // Notify the parent of any changes.
     if ($notify && isset($this->parent)) {
         $this->parent->onChange($this->name);
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $field_definition = BaseFieldDefinition::create('plugin:plugin_test_helper_mock');
     /** @var \Drupal\Core\Field\FieldItemListInterface $field_item_list */
     $field_item_list = \Drupal::typedDataManager()->create($field_definition);
     $field_item_list->appendItem();
     $this->fieldItem = $field_item_list->first();
 }
Esempio n. 10
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
     $constraints = parent::getConstraints();
     if ($max_length = $this->getSetting('max_length')) {
         $constraints[] = $constraint_manager->create('ComplexData', array('value' => array('Length' => array('max' => $max_length, 'maxMessage' => t('%name: the text may not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length))))));
     }
     return $constraints;
 }
Esempio n. 11
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraints = parent::getConstraints();
     // If this is an unsigned integer, add a validation constraint for the
     // integer to be positive.
     if ($this->getSetting('unsigned')) {
         $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
         $constraints[] = $constraint_manager->create('ComplexData', array('value' => array('Range' => array('min' => 0, 'minMessage' => t('%name: The integer must be larger or equal to %min.', array('%name' => $this->getFieldDefinition()->getLabel(), '%min' => 0))))));
     }
     return $constraints;
 }
 /**
  * Sets the value of a particular configuration option.
  *
  * @param string $name
  *   The name of the configuration option to set.
  * @param mixed $value
  *   The value to set.
  *
  * @todo This doesn't belong here. Move this into a new base class in
  *   https://www.drupal.org/node/1764380.
  * @todo This does not set a value in \Drupal::config(), so the name is confusing.
  *
  * @return \Drupal\Core\Executable\ExecutablePluginBase.
  *   The executable object for chaining.
  */
 public function setConfig($key, $value)
 {
     if ($definition = $this->getConfigDefinition($key)) {
         $typed_data = \Drupal::typedDataManager()->create($definition, $value);
         if ($typed_data->validate()->count() > 0) {
             throw new PluginException("The provided configuration value does not pass validation.");
         }
     }
     $this->configuration[$key] = $value;
     return $this;
 }
Esempio n. 13
0
 /**
  * {@inheritdoc}
  */
 public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL)
 {
     parent::__construct($definition, $name, $parent);
     // Initialize computed properties by default, such that they get cloned
     // with the whole item.
     foreach ($this->definition->getPropertyDefinitions() as $name => $definition) {
         if ($definition->isComputed()) {
             $this->properties[$name] = \Drupal::typedDataManager()->getPropertyInstance($this, $name);
         }
     }
 }
Esempio n. 14
0
 /**
  * {@inheritdoc}
  */
 public function getClass()
 {
     $class = isset($this->definition['class']) ? $this->definition['class'] : NULL;
     if (!empty($class)) {
         return $class;
     } else {
         // If a list definition is used but no class has been specified, derive
         // the default list class from the item type.
         $item_type_definition = \Drupal::typedDataManager()->getDefinition($this->getItemDefinition()->getDataType());
         if (!$item_type_definition) {
             throw new \LogicException("An invalid data type '{$this->getItemDefinition()->getDataType()}' has been specified for list items");
         }
         return $item_type_definition['list_class'];
     }
 }
Esempio n. 15
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
     $constraints = parent::getConstraints();
     $settings = $this->getSettings();
     $label = $this->getFieldDefinition()->getLabel();
     if (!empty($settings['min'])) {
         $min = $settings['min'];
         $constraints[] = $constraint_manager->create('ComplexData', array('value' => array('Range' => array('min' => $min, 'minMessage' => t('%name: the value may be no less than %min.', array('%name' => $label, '%min' => $min))))));
     }
     if (!empty($settings['max'])) {
         $max = $settings['max'];
         $constraints[] = $constraint_manager->create('ComplexData', array('value' => array('Range' => array('max' => $max, 'maxMessage' => t('%name: the value may be no greater than %max.', array('%name' => $label, '%max' => $max))))));
     }
     return $constraints;
 }
Esempio n. 16
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
     $constraints = parent::getConstraints();
     $settings = $this->getSettings();
     $label = $this->getFieldDefinition()->getLabel();
     if (!empty($settings['opacity'])) {
         $min = 0;
         $constraints[] = $constraint_manager->create('ComplexData', array('opacity' => array('Range' => array('min' => $min, 'minMessage' => t('%name: the opacity may be no less than %min.', array('%name' => $label, '%min' => $min))))));
         $max = 1;
         $constraints[] = $constraint_manager->create('ComplexData', array('opacity' => array('Range' => array('max' => $max, 'maxMessage' => t('%name: the opacity may be no greater than %max.', array('%name' => $label, '%max' => $max))))));
     }
     // @todo: Adapt constraint based on storage.
     //$constraints[] = $constraint_manager->create('ComplexData', array(
     //  'color' => array(
     //    'Regex' => array(
     //      'pattern' => '/^#(\d+)$/i',
     //    )
     //  ),
     //));
     return $constraints;
 }
Esempio n. 17
0
 /**
  * {@inheritdoc}
  */
 public function getTypedData()
 {
     if (!isset($this->typedData)) {
         $class = \Drupal::typedDataManager()->getDefinition('entity')['class'];
         $this->typedData = $class::createFromEntity($this);
     }
     return $this->typedData;
 }
Esempio n. 18
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraints = parent::getConstraints();
     list($current_handler) = explode(':', $this->getSetting('handler'), 2);
     if ($current_handler === 'default') {
         $handler_settings = $this->getSetting('handler_settings');
         if (!empty($handler_settings['target_bundles'])) {
             $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
             $constraints[] = $constraint_manager->create('ComplexData', ['entity' => ['Bundle' => ['bundle' => $handler_settings['target_bundles']]]]);
         }
     }
     return $constraints;
 }
 /**
  * Tests the function of the typed data type.
  */
 function testTypedDataAPI()
 {
     $definition = DataDefinition::create('filter_format');
     $data = \Drupal::typedDataManager()->create($definition);
     $this->assertTrue($data instanceof OptionsProviderInterface, 'Typed data object implements \\Drupal\\Core\\TypedData\\OptionsProviderInterface');
     $filtered_html_user = $this->createUser(array('uid' => 2), array(entity_load('filter_format', 'filtered_html')->getPermissionName()));
     // Test with anonymous user.
     $user = new AnonymousUserSession();
     \Drupal::currentUser()->setAccount($user);
     $expected_available_options = array('filtered_html' => 'Filtered HTML', 'full_html' => 'Full HTML', 'filter_test' => 'Test format', 'plain_text' => 'Plain text');
     $available_values = $data->getPossibleValues();
     $this->assertEqual($available_values, array_keys($expected_available_options));
     $available_options = $data->getPossibleOptions();
     $this->assertEqual($available_options, $expected_available_options);
     $allowed_values = $data->getSettableValues($user);
     $this->assertEqual($allowed_values, array('plain_text'));
     $allowed_options = $data->getSettableOptions($user);
     $this->assertEqual($allowed_options, array('plain_text' => 'Plain text'));
     $data->setValue('foo');
     $violations = $data->validate();
     $this->assertFilterFormatViolation($violations, 'foo');
     // Make sure the information provided by a violation is correct.
     $violation = $violations[0];
     $this->assertEqual($violation->getRoot(), $data, 'Violation root is filter format.');
     $this->assertEqual($violation->getPropertyPath(), '', 'Violation property path is correct.');
     $this->assertEqual($violation->getInvalidValue(), 'foo', 'Violation contains invalid value.');
     $data->setValue('plain_text');
     $violations = $data->validate();
     $this->assertEqual(count($violations), 0, "No validation violation for format 'plain_text' found");
     // Anonymous doesn't have access to the 'filtered_html' format.
     $data->setValue('filtered_html');
     $violations = $data->validate();
     $this->assertFilterFormatViolation($violations, 'filtered_html');
     // Set user with access to 'filtered_html' format.
     \Drupal::currentUser()->setAccount($filtered_html_user);
     $violations = $data->validate();
     $this->assertEqual(count($violations), 0, "No validation violation for accessible format 'filtered_html' found.");
     $allowed_values = $data->getSettableValues($filtered_html_user);
     $this->assertEqual($allowed_values, array('filtered_html', 'plain_text'));
     $allowed_options = $data->getSettableOptions($filtered_html_user);
     $expected_allowed_options = array('filtered_html' => 'Filtered HTML', 'plain_text' => 'Plain text');
     $this->assertEqual($allowed_options, $expected_allowed_options);
 }
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraints = parent::getConstraints();
     // Check that the number of values doesn't exceed the field cardinality. For
     // form submitted values, this can only happen with 'multiple value'
     // widgets.
     $cardinality = $this->getFieldDefinition()->getFieldStorageDefinition()->getCardinality();
     if ($cardinality != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
         $constraints[] = \Drupal::typedDataManager()->getValidationConstraintManager()->create('Count', array('max' => $cardinality, 'maxMessage' => t('%name: this field cannot hold more than @count values.', array('%name' => $this->getFieldDefinition()->getLabel(), '@count' => $cardinality))));
     }
     return $constraints;
 }
 /**
  * Creates a new data reference definition.
  *
  * @param string $target_data_type
  *   The data type of the referenced data.
  *
  * @return static
  */
 public static function create($target_data_type)
 {
     // This assumes implementations use a "TYPE_reference" naming pattern.
     $definition = parent::create($target_data_type . '_reference');
     return $definition->setTargetDefinition(\Drupal::typedDataManager()->createDataDefinition($target_data_type));
 }
Esempio n. 22
0
 /**
  * Tests the typedDataManager() method.
  *
  * @covers ::typedDataManager
  */
 public function testTypedDataManager()
 {
     $this->setMockContainerService('typed_data_manager');
     $this->assertNotNull(\Drupal::typedDataManager());
 }
Esempio n. 23
0
 /**
  * {@inheritdoc}
  */
 protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definition, $batch_size)
 {
     // Check whether the whole field storage definition is gone, or just some
     // bundle fields.
     $storage_definition = $field_definition->getFieldStorageDefinition();
     $is_deleted = $this->storageDefinitionIsDeleted($storage_definition);
     $table_mapping = $this->getTableMapping();
     $table_name = $table_mapping->getDedicatedDataTableName($storage_definition, $is_deleted);
     // Get the entities which we want to purge first.
     $entity_query = $this->database->select($table_name, 't', array('fetch' => \PDO::FETCH_ASSOC));
     $or = $entity_query->orConditionGroup();
     foreach ($storage_definition->getColumns() as $column_name => $data) {
         $or->isNotNull($table_mapping->getFieldColumnName($storage_definition, $column_name));
     }
     $entity_query->distinct(TRUE)->fields('t', array('entity_id'))->condition('bundle', $field_definition->getTargetBundle())->range(0, $batch_size);
     // Create a map of field data table column names to field column names.
     $column_map = array();
     foreach ($storage_definition->getColumns() as $column_name => $data) {
         $column_map[$table_mapping->getFieldColumnName($storage_definition, $column_name)] = $column_name;
     }
     $entities = array();
     $items_by_entity = array();
     foreach ($entity_query->execute() as $row) {
         $item_query = $this->database->select($table_name, 't', array('fetch' => \PDO::FETCH_ASSOC))->fields('t')->condition('entity_id', $row['entity_id'])->orderBy('delta');
         foreach ($item_query->execute() as $item_row) {
             if (!isset($entities[$item_row['revision_id']])) {
                 // Create entity with the right revision id and entity id combination.
                 $item_row['entity_type'] = $this->entityTypeId;
                 // @todo: Replace this by an entity object created via an entity
                 // factory, see https://www.drupal.org/node/1867228.
                 $entities[$item_row['revision_id']] = _field_create_entity_from_ids((object) $item_row);
             }
             $item = array();
             foreach ($column_map as $db_column => $field_column) {
                 $item[$field_column] = $item_row[$db_column];
             }
             $items_by_entity[$item_row['revision_id']][] = $item;
         }
     }
     // Create field item objects and return.
     foreach ($items_by_entity as $revision_id => $values) {
         $entity_adapter = $entities[$revision_id]->getTypedData();
         $items_by_entity[$revision_id] = \Drupal::typedDataManager()->create($field_definition, $values, $field_definition->getName(), $entity_adapter);
     }
     return $items_by_entity;
 }
 /**
  * Tests validation constraints provided by the Entity API.
  */
 public function testEntityConstraintValidation()
 {
     $entity = $this->createTestEntity('entity_test');
     $entity->save();
     // Create a reference field item and let it reference the entity.
     $definition = BaseFieldDefinition::create('entity_reference')->setLabel('Test entity')->setSetting('target_type', 'entity_test');
     $reference_field = \Drupal::typedDataManager()->create($definition);
     $reference = $reference_field->first()->get('entity');
     $reference->setValue($entity);
     // Test validation the typed data object.
     $violations = $reference->validate();
     $this->assertEqual($violations->count(), 0);
     // Test validating an entity of the wrong type.
     $user = $this->createUser();
     $user->save();
     $node = entity_create('node', array('type' => 'page', 'uid' => $user->id()));
     $reference->setValue($node);
     $violations = $reference->validate();
     $this->assertEqual($violations->count(), 1);
     // Test bundle validation.
     NodeType::create(array('type' => 'article'))->save();
     $definition = BaseFieldDefinition::create('entity_reference')->setLabel('Test entity')->setSettings(array('target_type' => 'node', 'target_bundle' => 'article'));
     $reference_field = \Drupal::TypedDataManager()->create($definition);
     $reference = $reference_field->first()->get('entity');
     $reference->setValue($node);
     $violations = $reference->validate();
     $this->assertEqual($violations->count(), 1);
     $node = entity_create('node', array('type' => 'article', 'uid' => $user->id()));
     $node->save();
     $reference->setValue($node);
     $violations = $reference->validate();
     $this->assertEqual($violations->count(), 0);
 }
Esempio n. 25
0
 /**
  * Helper to retrieve the field item class.
  *
  * @todo: Remove once getClass() adds in defaults. See
  * https://drupal.org/node/2116341.
  */
 protected function getFieldItemClass()
 {
     if ($class = $this->getItemDefinition()->getClass()) {
         return $class;
     } else {
         $type_definition = \Drupal::typedDataManager()->getDefinition($this->getItemDefinition()->getDataType());
         return $type_definition['class'];
     }
 }
Esempio n. 26
0
 /**
  * Helper to retrieve the field item class.
  */
 protected function getFieldItemClass()
 {
     $type_definition = \Drupal::typedDataManager()->getDefinition('field_item:' . $this->getType());
     return $type_definition['class'];
 }
Esempio n. 27
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraints = isset($this->definition['constraints']) ? $this->definition['constraints'] : array();
     $constraints += \Drupal::typedDataManager()->getDefaultConstraints($this);
     return $constraints;
 }
Esempio n. 28
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     $constraints = parent::getConstraints();
     $manager = \Drupal::typedDataManager()->getValidationConstraintManager();
     $availableCountries = $this->getAvailableCountries();
     $enabledFields = array_filter($this->getSetting('fields'));
     $constraints[] = $manager->create('Country', ['availableCountries' => $availableCountries]);
     $constraints[] = $manager->create('AddressFormat', ['fields' => $enabledFields]);
     return $constraints;
 }
Esempio n. 29
0
 /**
  * {@inheritdoc}
  */
 public function getConstraints()
 {
     return \Drupal::typedDataManager()->getDefaultConstraints($this) + $this->constraints;
 }
Esempio n. 30
0
 /**
  * {@inheritdoc}
  */
 public function validate()
 {
     // @todo: Add the typed data manager as proper dependency.
     return \Drupal::typedDataManager()->getValidator()->validate($this);
 }