示例#1
0
 public function unhandleField($entity_type, $field_type, $field_name, &$value)
 {
     if (!is_array($value)) {
         return;
     }
     if (!array_key_exists('url', $value)) {
         return;
     }
     if (!array_key_exists('original_path', $value)) {
         return;
     }
     if (!array_key_exists('uuid', $value)) {
         return;
     }
     if (!array_key_exists('uid', $value)) {
         return;
     }
     // Make sure a file doesn't already exist with that UUID.
     $entity = Entity::loadByUUID($value['uuid'], 'file');
     if ($entity) {
         $definition = $entity->definition;
     } else {
         // Make sure a file doesn't already exist with that URI.
         $query = db_select('file_managed', 'f');
         $query->addField('f', 'fid');
         $query->condition('f.uri', $value['original_path']);
         $query->range(0, 1);
         $result = $query->execute()->fetch();
         if ($result) {
             $entity = Entity::load($result->fid, 'file');
             if ($entity) {
                 $definition = $entity->definition;
             }
         }
     }
     // If we haven't found the file yet, upload it.
     if (!isset($definition)) {
         // Decode the contents of the file.
         $contents = file_get_contents($value['url']);
         if ($contents === false) {
             throw new FileHandlerException('There was an error fetching the contents of the file.');
         }
         // Save the file.
         $file = file_save_data($contents, $value['original_path']);
         if (!$file || !$file->fid) {
             throw new FileHandlerException('There was an error saving the file to the database.');
         }
         $file->uuid = $value['uuid'];
         $file->uid = $value['uid'];
         file_save($file);
         $definition = $file;
     }
     // Don't completely reset the entity.
     foreach ((array) $definition as $key => $val) {
         $this->unresolved_definition->{$key} = $val;
     }
 }
 protected function handleIndividualValue($entity_type, $field_type, $field_name, &$value, $delta)
 {
     if (!is_array($value)) {
         return;
     }
     if (!array_key_exists('fid', $value)) {
         throw new ImageCropFieldHandlerException('The field ' . $field_name . ' does not have a file ID.');
     }
     $entity = Entity::load($value['fid'], 'file');
     if (!$entity) {
         throw new FileReferenceHandlerException('The field ' . $field_name . ' has no value.');
     }
     $value['fid'] = self::createReferenceDefinition($entity);
     $this->addDependency($entity);
     // Get the original image and store it.
     $source_file = _imagefield_crop_file_to_crop($value['fid']);
     if (!$source_file) {
         throw new ImageCropFieldHandlerException('The field ' . $field_name . ' does not have a valid source file ID.');
     }
     $source_file_entity = Entity::load($source_file->fid, 'file');
     if (!$source_file_entity) {
         throw new ImageCropFieldHandlerException('The field ' . $field_name . ' has no source image.');
     }
     $value['source_fid'] = self::createReferenceDefinition($source_file_entity);
     $this->addDependency($source_file_entity);
     // Handle the UID now.
     if (array_key_exists('uid', $value)) {
         $entity = Entity::load($value['uid'], 'user');
         if (!$entity) {
             throw new FileReferenceHandlerException('The field ' . $field_name . ' is associated with a user that doesn\'t exist.');
         }
         $value['uid'] = self::createReferenceDefinition($entity);
         $this->addDependency($entity);
     }
     // Make sure the dimensions of the cropbox are always set.
     if (isset($this->entity->definition->{$field_name})) {
         $original_entity_definition_language = reset($this->entity->definition->{$field_name});
         // First language.
         if (array_key_exists($delta, $original_entity_definition_language)) {
             $original_entity_definition = $original_entity_definition_language[$delta];
             if (!array_key_exists('cropbox_x', $value)) {
                 $value['cropbox_x'] = array_key_exists('cropbox_x', $original_entity_definition) ? $original_entity_definition['cropbox_x'] : 0;
             }
             if (!array_key_exists('cropbox_y', $value)) {
                 $value['cropbox_y'] = array_key_exists('cropbox_y', $original_entity_definition) ? $original_entity_definition['cropbox_y'] : 0;
             }
             if (!array_key_exists('cropbox_width', $value)) {
                 $value['cropbox_width'] = array_key_exists('cropbox_width', $original_entity_definition) ? $original_entity_definition['cropbox_width'] : 0;
             }
             if (!array_key_exists('cropbox_height', $value)) {
                 $value['cropbox_height'] = array_key_exists('cropbox_height', $original_entity_definition) ? $original_entity_definition['cropbox_height'] : 0;
             }
         }
     }
 }
 public function execute(\EntityDrupalWrapper $entity_raw, $remote_raw, $force)
 {
     $remote = publisher_remote_load($remote_raw);
     if (!$remote) {
         return false;
     }
     $entity = Entity::load($entity_raw->getIdentifier(), $entity_raw->type());
     if (!$entity) {
         return false;
     }
     publisher_send_entity($entity, $remote, array('force' => $force));
     return true;
 }
 protected function handleIndividualValue($entity_type, $field_type, $field_name, &$value, $delta)
 {
     // Try to load the entity.
     if (!array_key_exists('tid', $value)) {
         throw new TaxonomyReferenceHandlerException('The term identifier did not exist on the entity.');
     }
     $entity = Entity::load($value['tid'], 'taxonomy_term');
     if (!$entity) {
         throw new TaxonomyReferenceHandlerException('The entity ' . $value['tid'] . ' does not exist.');
     }
     // Set the reference definition for the field.
     $value['tid'] = self::createReferenceDefinition($entity);
     $this->addDependency($entity, true);
     $this->addRelationship($this->original_entity, $entity, $field_name, $delta);
 }
 protected function handleIndividualValue($entity_type, $field_type, $field_name, &$value, $delta)
 {
     // Get the information for the specific field.
     $target_type = $this->getTargetType($field_name);
     if (!array_key_exists('target_id', $value)) {
         throw new EntityReferenceHandlerException('The field ' . $field_name . ' does not have a target ID.');
     }
     $entity = Entity::load($value['target_id'], $target_type);
     if (!$entity) {
         throw new EntityReferenceHandlerException('The field ' . $field_name . ' has no value.');
     }
     $value['target_id'] = self::createReferenceDefinition($entity);
     $this->addDependency($entity, true);
     $this->addRelationship($this->original_entity, $entity, $field_name, $delta);
 }
示例#6
0
 public function handleField($entity_type, $field_type, $field_name, &$value)
 {
     if ($entity_type == 'user' && ($value === 0 || $value === '0')) {
         return;
     }
     // We should be getting a number for the value.
     if (!is_numeric($value)) {
         throw new IDHandlerException('The field of type ' . $field_type . ' does not have a numeric value.');
     }
     // Load the entity.
     $entity = Entity::load($value, $entity_type);
     if (!$entity) {
         throw new IDHandlerException('The entity ' . $value . ' does not exist.');
     }
     // Update the value.
     $value = self::createReferenceDefinition($entity);
 }
示例#7
0
 public function handleField($entity_type, $field_type, $field_name, &$value)
 {
     if ($value === 0 || $value === '0') {
         return;
     }
     // Ignore anonymous user.
     // We should be getting a number for the value.
     if (!is_numeric($value)) {
         throw new UserHandlerException('The field of type ' . $field_type . ' does not have a numeric value.');
     }
     // Load the user entity.
     $entity = Entity::load($value, 'user');
     if (!$entity) {
         throw new UserHandlerException('The user ' . $value . ' does not exist.');
     }
     // Update the value.
     $value = self::createReferenceDefinition($entity);
     $this->addDependency($entity);
 }
示例#8
0
 public function resolve($recurse = true)
 {
     // Get all revisions for the entity.
     $diff = new EntityDiff($this->entity);
     $revisions = $diff->getRevisionHistory();
     if (is_array($revisions)) {
         // Make it so that we process the latest revision last.
         $revisions = array_reverse($revisions);
         foreach ($revisions as $revision_id) {
             $entity = Entity::load($this->base_entity->id(), $this->base_entity->type());
             $entity->setRevision($revision_id);
             $this->entity = $entity;
             $this->base_entity = clone $entity;
             $this->resolveDependencies($recurse, $entity->definition);
         }
     } else {
         $this->resolveDependencies($recurse);
     }
 }
 protected function handleIndividualValue($entity_type, $field_type, $field_name, &$value, $delta)
 {
     global $base_url;
     if (!is_array($value)) {
         throw new FileReferenceHandlerException('The field ' . $field_name . ' has an invalid value.');
     }
     if (!array_key_exists('fid', $value)) {
         throw new FileReferenceHandlerException('The field ' . $field_name . ' does not have a file ID.');
     }
     $entity = Entity::load($value['fid'], 'file');
     if (!$entity) {
         throw new FileReferenceHandlerException('The field ' . $field_name . ' has no value.');
     }
     if (!file_exists(str_replace($base_url, DRUPAL_ROOT, urldecode(file_create_url($entity->definition->uri))))) {
         drupal_set_message(t('The file in the field !field does not exist. Setting the value to <code>NULL</code>', array('!field' => $field_name)), 'warning');
         $value = null;
         return;
     }
     $value['fid'] = self::createReferenceDefinition($entity);
     $this->addDependency($entity, true);
     $this->addRelationship($this->original_entity, $entity, $field_name, $delta, array('alt' => !empty($value['alt']) ? $value['alt'] : '', 'title' => !empty($value['title']) ? $value['title'] : '', 'display' => !empty($value['display']) ? $value['display'] : true, 'description' => !empty($value['description']) ? $value['description'] : ''));
 }
示例#10
0
 protected function handleSingle($mlid, $field_type)
 {
     if (!is_numeric($mlid)) {
         throw new MenuLinkHandlerException('The link ID ' . $mlid . ' is not numeric.');
     }
     $entity = Entity::load($mlid, 'menu_link');
     if (!$entity) {
         throw new MenuLinkHandlerException('The entity ' . $mlid . ' does not exist.');
     }
     if ($entity->uuid() == $this->entity->uuid()) {
         return 'self';
     }
     if ($field_type == 'menu_links') {
         // If the menu item is associated with the node, make the menu item
         // depend on the node, not the other way around.
         $sources = array($entity->uuid());
         $this->addDependency($this->original_entity, $sources);
     }
     // Don't create a source of the node if the type is menu links.
     $this->addDependency($entity, $field_type !== 'menu_links');
     return self::createReferenceDefinition($entity);
 }
 protected function urlToFileEntity($url)
 {
     $url = urldecode(DRUPAL_ROOT . EntityPathHelper::normalizeUrl($url));
     if (file_exists($url)) {
         // Get the filename.
         $filename = pathinfo($url, PATHINFO_FILENAME) . '.' . pathinfo($url, PATHINFO_EXTENSION);
         $filesize = filesize($url);
         $files = db_select('file_managed', 'f')->fields('f', array('fid', 'uri', 'filesize'))->condition('filename', $filename)->condition('filesize', $filesize)->execute();
         $found_fid = -1;
         while ($row = $files->fetch()) {
             $result_uri = drupal_realpath($row->uri);
             if ($result_uri == drupal_realpath($url)) {
                 $found_fid = $row->fid;
                 break;
             }
         }
         if ($found_fid !== -1) {
             return Entity::load($found_fid, 'file');
         } else {
             // Create the file entity.
             if ($contents = file_get_contents($url)) {
                 $public_files_directory = DRUPAL_ROOT . '/' . variable_get('file_public_path', conf_path() . '/files') . '/';
                 $schema_url = 'public://' . str_replace($public_files_directory, '', $url);
                 // This will basically re-create the same file with the same filename, so we don't
                 // need to check to see if the file already exists because we don't care to replace
                 // the file with itself.
                 $file = file_save_data($contents, $schema_url, FILE_EXISTS_REPLACE);
                 return Entity::load($file->fid, 'file');
             }
         }
     }
     return false;
 }