diff --git a/.travis-before-script.sh b/.travis-before-script.sh index c71c1fc..1c3243a 100644 --- a/.travis-before-script.sh +++ b/.travis-before-script.sh @@ -16,3 +16,4 @@ cd "$DRUPAL_TI_MODULES_PATH" # Download Pathauto 8.x-1.x git clone --depth 1 --branch 8.x-1.x https://github.com/md-systems/pathauto.git git clone --depth 1 --branch 8.x-1.x http://git.drupal.org/project/token.git +git clone --depth 1 --branch 8.x-3.x http://git.drupal.org/project/ctools.git diff --git a/file_entity.install b/file_entity.install index c5513b7..9b15096 100644 --- a/file_entity.install +++ b/file_entity.install @@ -50,8 +50,16 @@ function file_entity_schema() { * Implements hook_install(). */ function file_entity_install() { - $type_storage_definition = \Drupal::entityManager()->getFieldStorageDefinitions('file')['type']; - \Drupal::entityManager()->getStorage('file')->onFieldStorageDefinitionCreate($type_storage_definition); + /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */ + $entity_field_manager = \Drupal::service('entity_field.manager'); + $type_storage_definition = $entity_field_manager->getFieldStorageDefinitions('file')['type']; + \Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('type', 'file', 'file_entity', $type_storage_definition); + + // If the pathauto module already exists, update the entity schema. + if (\Drupal::moduleHandler()->moduleExists('pathauto')) { + $path_storage_definition = $entity_field_manager->getFieldStorageDefinitions('file')['path']; + \Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('path', 'file', 'file_entity', $path_storage_definition); + } // Set permissions. $roles = user_roles(); @@ -59,11 +67,6 @@ function file_entity_install() { user_role_grant_permissions($rid, array('view files')); } - // Configure default pathauto variables if it is currently installed. - if (\Drupal::moduleHandler()->moduleExists('pathauto')) { - \Drupal::configFactory()->getEditable('pathauto.pattern')->set('patterns.file.default', 'files/[file:name]')->save(); - } - // Classify existing files according to the currently defined file types. // Queue all files to be classified during cron runs using the Queue API. $queue = \Drupal::queue('file_entity_type_determine'); diff --git a/src/Plugin/pathauto/AliasType/FileAliasType.php b/src/Plugin/pathauto/AliasType/FileAliasType.php deleted file mode 100644 index 50cb75c..0000000 --- a/src/Plugin/pathauto/AliasType/FileAliasType.php +++ /dev/null @@ -1,99 +0,0 @@ -t('Default path pattern (applies to all files with blank patterns below)'); - } - - /** - * {@inheritdoc} - */ - public function defaultConfiguration() { - return array('default' => array('files/[file:name]')) + parent::defaultConfiguration(); - } - - /** - * {@inheritdoc} - */ - public function batchUpdate(&$context) { - if (!isset($context['sandbox']['current'])) { - $context['sandbox']['count'] = 0; - $context['sandbox']['current'] = 0; - } - - $query = db_select('file_managed', 'fm'); - $query->leftJoin('url_alias', 'ua', "CONCAT('file/', fm.fid) = ua.source"); - $query->addField('fm', 'fid'); - $query->isNull('ua.source'); - $query->condition('fm.fid', $context['sandbox']['current'], '>'); - $query->orderBy('fm.fid'); - $query->addTag('pathauto_bulk_update'); - $query->addMetaData('entity', 'file'); - - // Get the total amount of items to process. - if (!isset($context['sandbox']['total'])) { - $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField(); - - // If there are no files to update, the stop immediately. - if (!$context['sandbox']['total']) { - $context['finished'] = 1; - return; - } - } - - $query->range(0, 25); - $fids = $query->execute()->fetchCol(); - - $options = array('message' => FALSE); - - $files = File::loadMultiple($fids); - foreach ($files as $file) { - \Drupal::service('pathauto.manager')->updateAlias($file, 'bulkupdate', $options); - } - - if (!empty($options['message'])) { - drupal_set_message(\Drupal::translation()->formatPlural(count($fids), 'Updated URL alias for 1 file.', 'Updated URL aliases for @count files.')); - } - - $context['sandbox']['count'] += count($fids); - $context['sandbox']['current'] = max($fids); - $context['message'] = t('Updated alias for file @fid.', array('@fid' => end($fids))); - - if ($context['sandbox']['count'] != $context['sandbox']['total']) { - $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total']; - } - } - - /** - * {@inheritdoc} - */ - public function getSourcePrefix() { - return 'file/'; - } - -} diff --git a/src/Tests/FileEntityPathautoTest.php b/src/Tests/FileEntityPathautoTest.php index af5b25c..8ec1112 100644 --- a/src/Tests/FileEntityPathautoTest.php +++ b/src/Tests/FileEntityPathautoTest.php @@ -6,6 +6,9 @@ namespace Drupal\file_entity\Tests; +use Drupal\Component\Utility\Unicode; +use Drupal\pathauto\Entity\PathautoPattern; + /** * Tests Pathauto support. * @@ -26,14 +29,18 @@ class FileEntityPathautoTest extends FileEntityTestBase { * Tests Pathauto support. */ public function testPathauto() { - $this->config('pathauto.pattern') - ->set('patterns.file.default', 'files/[file:name]') - ->save(); + $pattern = PathautoPattern::create([ + 'id' => Unicode::strtolower($this->randomMachineName()), + 'type' => 'canonical_entities:file', + 'pattern' => '/files/[file:name]', + 'weight' => 0, + ]); + $pattern->save(); - $file = $this->createFileEntity(); + $file = $this->createFileEntity(['filename' => 'example.png']); $path = \Drupal::service('path.alias_storage')->load(array('source' => '/' . $file->urlInfo()->getInternalPath())); - $this->assertTrue($path, t('Alias for file found.')); + $this->assertEqual($path['alias'], '/files/example-png', t('Alias for file found.')); } }