From 197d8acd0ce8d5ff6c6e681c300cc68491f3f8e7 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Tue, 29 Oct 2019 17:11:39 -0300 Subject: [PATCH] Only copy a file if it's not in the Drupal web root. --- src/Commands/MediaAttributionCommands.php | 5 ++++- src/LicenseLoader.php | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Commands/MediaAttributionCommands.php b/src/Commands/MediaAttributionCommands.php index bc8dd7c..3fb829e 100644 --- a/src/Commands/MediaAttributionCommands.php +++ b/src/Commands/MediaAttributionCommands.php @@ -37,7 +37,10 @@ class MediaAttributionCommands extends DrushCommands { $license_data = Yaml::decode($file_contents); foreach ($license_data as $license_item) { - $icon_file_path = isset($license_item['icon_file']) ? $cwd . '/' . $license_item['icon_file'] : ''; + $icon_file_path = ''; + if (!empty($license_item['icon_file'])) { + $icon_file_path = substr($license_item['icon_file'], 0, 1) == '/' ? $license_item['icon_file'] : $cwd . '/' . $license_item['icon_file']; + } LicenseLoader::createOrUpdateLicenseTerm($license_item['title'],$license_item['short_label'], $icon_file_path, $license_item['url']); } diff --git a/src/LicenseLoader.php b/src/LicenseLoader.php index 8abf2c2..5aa500c 100644 --- a/src/LicenseLoader.php +++ b/src/LicenseLoader.php @@ -7,8 +7,7 @@ use Drupal\taxonomy\Entity\Term; use Drupal\file\Entity\File; use phpDocumentor\Reflection\Types\Integer; -class LicenseLoader -{ +class LicenseLoader { /** * Create a new license term with the given values. @@ -27,12 +26,11 @@ class LicenseLoader * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException * @throws \Drupal\Core\Entity\EntityStorageException */ - public static function createOrUpdateLicenseTerm($term_title, $term_short_label, $icon_file_path, $license_url) - { + public static function createOrUpdateLicenseTerm($term_title, $term_short_label, $icon_file_path, $license_url) { $tids = array_values(\Drupal::entityQuery('taxonomy_term') ->condition('name', $term_title) ->execute()); - print_r($tids); + if ($tids) { self::updateLicenseTerm($tids[0], $term_title, $term_short_label, $icon_file_path, $license_url); return $tids[0]; @@ -125,9 +123,16 @@ class LicenseLoader // if not create a file if (!$icon_file) { - $fs = \Drupal::service('file_system'); - $icon_uri = $fs->copy($icon_file_path, 'public://'); - + if (substr($icon_file_path, 0, strlen(DRUPAL_ROOT)) == DRUPAL_ROOT) { + $icon_uri = substr($icon_file_path, strlen(DRUPAL_ROOT), strlen($icon_file_path)); + } + elseif (substr($icon_file_path, 0, 1) != '/') { + $icon_uri = $icon_file_path; + } + else { + $fs = \Drupal::service('file_system'); + $icon_uri = $fs->copy($icon_file_path, 'public://'); + } $icon_file = File::create([ 'uri' => $icon_uri, ]);