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; }
protected function entitiesFromUrl($url, $alias = false) { $entities = EntityPathHelper::entitiesFromPath($url); foreach ($entities['entities'] as $index => $entity) { $this->addDependency($entity); $entities['entities'][$index] = self::createReferenceDefinition($entity); } $entities['alias'] = $alias; return $entities; }
public function receive($endpoint, $payload = array()) { if (!array_key_exists('associations', $payload) || !is_array($payload['associations'])) { throw new MalformedRequestException('There were no associations sent with the request.'); } $errors = 0; $success = 0; $unchanged = 0; foreach ($payload['associations'] as $menu_item) { $router_url = $menu_item['link_path']['router_url']; $entities = $menu_item['link_path']['entities']; $errored = false; foreach ($entities as $index => $entity) { try { $loaded_entity = HandlerBase::entityFromReferenceDefinition($entity); if (!$loaded_entity) { $errored = true; break; } $entities[$index] = $loaded_entity; } catch (InvalidReferenceDefinitionException $ex) { drupal_set_message(t('Error importing link @title, because "@reason."', array('@title' => $menu_item['link_title'], '@reason' => $ex->getMessage())), 'error'); $errored = true; break; } } if ($errored) { $errors++; continue; } $link_path = EntityPathHelper::pathFromEntities($router_url, $entities); if ($this->syncMenuItem($menu_item['menu_name'], $link_path, $menu_item['link_title'], $menu_item['hidden'], $menu_item['depth'], $menu_item['router_path'], $menu_item['uuid'])) { $success++; } else { $unchanged++; } } return array('counts' => array('errors' => $errors, 'success' => $success, 'unchanged' => $unchanged)); }