From 5812d011a6b31e6cd9ba10e222a423d38244971f Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 28 Oct 2019 17:08:27 -0300 Subject: [PATCH] WIP --- src/Commands/MediaAttributionCommands.php | 2 +- src/LicenseLoader.php | 115 +++++++++++++++++----- 2 files changed, 93 insertions(+), 24 deletions(-) diff --git a/src/Commands/MediaAttributionCommands.php b/src/Commands/MediaAttributionCommands.php index 8f3d29a..bc8dd7c 100644 --- a/src/Commands/MediaAttributionCommands.php +++ b/src/Commands/MediaAttributionCommands.php @@ -38,7 +38,7 @@ class MediaAttributionCommands extends DrushCommands { foreach ($license_data as $license_item) { $icon_file_path = isset($license_item['icon_file']) ? $cwd . '/' . $license_item['icon_file'] : ''; - LicenseLoader::createLicenseTerm($license_item['title'],$license_item['short_label'], $icon_file_path, $license_item['url']); + 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 1cd7a45..8abf2c2 100644 --- a/src/LicenseLoader.php +++ b/src/LicenseLoader.php @@ -7,7 +7,8 @@ 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. @@ -26,44 +27,112 @@ 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) + { + $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]; + } + else { + return self::createLicenseTerm($term_title, $term_short_label, $icon_file_path, $license_url); + } + } + + /** + * Craete a new license taxonomy term entity. + * + * @param $term_title + * @param $term_short_label + * @param $icon_file_path + * @param $license_url + * @return int + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * @throws \Drupal\Core\Entity\EntityStorageException + */ public static function createLicenseTerm($term_title, $term_short_label, $icon_file_path, $license_url) { if ($icon_file_path) { - // Just in case the file has already been created. - $icon_files = \Drupal::entityTypeManager() - ->getStorage('file') - ->loadByProperties(['uri' => $icon_file_path]); - $icon_file = reset($icon_files); - // if not create a file - - if (!$icon_file) { - $fs = \Drupal::service('file_system'); - $icon_uri = $fs->copy($icon_file_path, 'public://'); - - $icon_file = File::create([ - 'uri' => $icon_uri, - ]); - $icon_file->save(); - } + $icon_file = self::updateLicenseIcon($icon_file_path); $tid = Term::create([ - 'name' => $term_title, + 'name' => $term_title, 'vid' => 'media_attribution_licenses', - 'field_license_link' => ['title' => $term_short_label, 'uri' => $license_url], + 'field_license_link' => ['title' => $term_short_label, 'uri' => $license_url], 'field_license_icon' => [ 'target_id' => $icon_file->id(), 'alt' => $term_title, 'title' => $term_title, ] ])->save(); - } - else { + } else { // Create the term without an image. $tid = Term::create([ - 'name' => $term_title, + 'name' => $term_title, 'vid' => 'media_attribution_licenses', - 'field_license_link' => ['title' => $term_short_label, 'uri' => $license_url], + 'field_license_link' => ['title' => $term_short_label, 'uri' => $license_url], ])->save(); } return $tid; } + + /** + * Update an existing license term. + * + * @param $tid + * @param $term_title + * @param $term_short_label + * @param $icon_file_path + * @param $license_url + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * @throws \Drupal\Core\Entity\EntityStorageException + */ + public static function updateLicenseTerm($tid, $term_title, $term_short_label, $icon_file_path, $license_url) { + $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid); + $term->set('name', $term_title); + $term->set('field_license_link', ['title' => $term_short_label, 'uri' => $license_url]); + if ($icon_file_path) { + $icon_file = self::updateLicenseIcon($icon_file_path); + + $term->set('field_license_icon', [ + 'target_id' => $icon_file->id(), + 'alt' => $term_title, + 'title' => $term_title, + ]); + } + $term->save(); + } + + /** + * Create or update a license icon file. + * + * @param $icon_file_path + * @return array + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * @throws \Drupal\Core\Entity\EntityStorageException + */ + public static function updateLicenseIcon($icon_file_path) { +// Just in case the file has already been created. + $icon_files = \Drupal::entityTypeManager() + ->getStorage('file') + ->loadByProperties(['uri' => $icon_file_path]); + $icon_file = reset($icon_files); + // if not create a file + + if (!$icon_file) { + $fs = \Drupal::service('file_system'); + $icon_uri = $fs->copy($icon_file_path, 'public://'); + + $icon_file = File::create([ + 'uri' => $icon_uri, + ]); + $icon_file->save(); + } + return $icon_file; + } } \ No newline at end of file