/** * {@inheritdoc} */ public function preSave() { parent::preSave(); $entity = $this->getEntity(); if ($this->pre_hashed) { // Reset the pre_hashed value since it has now been used. $this->pre_hashed = FALSE; } elseif ($entity->isNew() || strlen(trim($this->value)) > 0 && $this->value != $entity->original->{$this->getFieldDefinition()->getName()}->value) { // Allow alternate password hashing schemes. $this->value = \Drupal::service('password')->hash(trim($this->value)); // Abort if the hashing failed and returned FALSE. if (!$this->value) { throw new EntityMalformedException('The entity does not have a password.'); } } if (!$entity->isNew()) { // If the password is empty, that means it was not changed, so use the // original password. if (empty($this->value)) { $this->value = $entity->original->{$this->getFieldDefinition()->getName()}->value; } } // Ensure that the existing password is unset to minimise risks of it // getting serialized and stored somewhere. $this->existing = NULL; }
/** * {@inheritdoc} */ public function isEmpty() { $value = $this->getValue(); if (!isset($value['value']) || $value['value'] === '') { return TRUE; } return parent::isEmpty(); }
/** * {@inheritdoc} */ public static function generateSampleValue(FieldDefinitionInterface $field_definition) { $values = parent::generateSampleValue($field_definition); $suffix_length = $field_definition->getSetting('max_length') - 7; foreach ($values as $key => $value) { $values[$key] = 'http://' . Unicode::substr($value, 0, $suffix_length); } return $values; }
/** * {@inheritdoc} */ public function preSave() { parent::preSave(); $entity = $this->getEntity(); // Update the user password if it has changed. if ($entity->isNew() || $this->value && $this->value != $entity->original->{$this->getFieldDefinition()->getName()}->value) { // Allow alternate password hashing schemes. $this->value = \Drupal::service('password')->hash(trim($this->value)); // Abort if the hashing failed and returned FALSE. if (!$this->value) { throw new EntityMalformedException('The entity does not have a password.'); } } if (!$entity->isNew()) { // If the password is empty, that means it was not changed, so use the // original password. if (empty($this->value)) { $this->value = $entity->original->{$this->getFieldDefinition()->getName()}->value; } } }
/** * {@inheritdoc} */ public static function schema(FieldStorageDefinitionInterface $field_definition) { $schema = parent::schema($field_definition); $schema['unique keys']['value'] = array('value'); return $schema; }
/** * Helper method to mock all store definitions. */ protected function setupFieldStorageDefinition() { $id_field_storage_definition = $this->getMock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface'); $id_field_storage_definition->expects($this->any())->method('getSchema')->willReturn(IntegerItem::schema($id_field_storage_definition)); $uuid_field_storage_definition = $this->getMock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface'); $uuid_field_storage_definition->expects($this->any())->method('getSchema')->willReturn(UuidItem::schema($uuid_field_storage_definition)); $type_field_storage_definition = $this->getMock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface'); $type_field_storage_definition->expects($this->any())->method('getSchema')->willReturn(StringItem::schema($type_field_storage_definition)); $langcode_field_storage_definition = $this->getMock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface'); $langcode_field_storage_definition->expects($this->any())->method('getSchema')->willReturn(LanguageItem::schema($langcode_field_storage_definition)); $name_field_storage_definition = $this->getMock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface'); $name_field_storage_definition->expects($this->any())->method('getSchema')->willReturn(StringItem::schema($name_field_storage_definition)); $description_field_storage_definition = $this->getMock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface'); $description_field_storage_definition->expects($this->any())->method('getSchema')->willReturn(TextLongItem::schema($description_field_storage_definition)); $homepage_field_storage_definition = $this->getMock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface'); $homepage_field_storage_definition->expects($this->any())->method('getSchema')->willReturn(UriItem::schema($homepage_field_storage_definition)); // Setup the user_id entity reference field. $this->entityManager->expects($this->any())->method('getDefinition')->willReturnMap([['user', TRUE, static::userEntityInfo()]]); $user_id_field_storage_definition = $this->getMock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface'); $user_id_field_storage_definition->expects($this->any())->method('getSetting')->with('target_type')->willReturn('user'); $user_id_field_storage_definition->expects($this->any())->method('getSettings')->willReturn(['target_type' => 'user']); $user_id_field_storage_definition->expects($this->any())->method('getSchema')->willReturn(EntityReferenceItem::schema($user_id_field_storage_definition)); $revision_id_field_storage_definition = $this->getMock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface'); $revision_id_field_storage_definition->expects($this->any())->method('getSchema')->willReturn(IntegerItem::schema($revision_id_field_storage_definition)); $this->entityManager->expects($this->any())->method('getFieldStorageDefinitions')->willReturn(['id' => $id_field_storage_definition, 'uuid' => $uuid_field_storage_definition, 'type' => $type_field_storage_definition, 'langcode' => $langcode_field_storage_definition, 'name' => $name_field_storage_definition, 'description' => $description_field_storage_definition, 'homepage' => $homepage_field_storage_definition, 'user_id' => $user_id_field_storage_definition, 'revision_id' => $revision_id_field_storage_definition]); }
/** * {@inheritdoc} */ public static function defaultSettings() { return array('max_length' => 2048) + parent::defaultSettings(); }