diff --git a/README.md b/README.md index 57bac56..91bd6a7 100644 --- a/README.md +++ b/README.md @@ -51,3 +51,15 @@ https://wiki.creativecommons.org/wiki/best_practices_for_attribution Installing the module creates License taxonomy term entries for all of the use variations of the international Creative Commons licenses. These can be edited, and other license types can be added as needed. + +## Bulk-loading Licenses via Drush + +Run `drush help media_attribution:load_licenses` for how to load license data from a YAML file. + +# Authors + +Alexander O'Neill ([alxp](https://drupal.org/u/alxp)) + +# Sponsoring Organization + +[University of Prince Edward Island Robertson Library](https://library.upei.ca/) \ No newline at end of file diff --git a/composer.json b/composer.json index 1e4ec82..070d57f 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,9 @@ "name": "drupal/media_attribution", "type": "drupal-module", "description": "Attach an attribution and license info to embedded media", - "keywords": ["Drupal"], + "keywords": [ + "Drupal" + ], "license": "GPL-2.0+", "homepage": "https://www.drupal.org/project/media_attribution", "minimum-stability": "dev", @@ -10,5 +12,17 @@ "issues": "https://www.drupal.org/project/issues/media_attribution", "source": "http://cgit.drupalcode.org/media_attribution" }, - "require": { } + "autoload": { + "psr-4": { + "Drupal\\media_attribution\\": "src/" + } + }, + "require": {}, + "extra": { + "drush": { + "services": { + "drush.services.yml": "^9" + } + } + } } diff --git a/drush.services.yml b/drush.services.yml new file mode 100644 index 0000000..4c4be05 --- /dev/null +++ b/drush.services.yml @@ -0,0 +1,5 @@ +services: + media_attribution.commands: + class: \Drupal\media_attribution\Commands\MediaAttributionCommands + tags: + - { name: drush.command } \ No newline at end of file diff --git a/media_attribution.default.licenses.yml b/media_attribution.default.licenses.yml index df48164..9dea673 100644 --- a/media_attribution.default.licenses.yml +++ b/media_attribution.default.licenses.yml @@ -2,30 +2,30 @@ - title: Attribution 4.0 International (CC BY 4.0) short_label: CC BY 4.0 - icon_file: by.png + icon_file: images/by.png url: https://creativecommons.org/licenses/by/4.0/ - title: Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) short_label: CC BY-NC 4.0 - icon_file: by-nc.png + icon_file: images/by-nc.png url: https://creativecommons.org/licenses/by-nc/4.0/ - title: Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0) short_label: CC BY-ND 4.0 - icon_file: by-nd.png + icon_file: images/by-nd.png url: https://creativecommons.org/licenses/by-nd/4.0/ - title: Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) short_label: CC BY-SA 4.0 - icon_file: by-sa.png + icon_file: images/by-sa.png url: https://creativecommons.org/licenses/by-sa/4.0/ - title: Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) short_label: CC BY-NC-ND 4.0 - icon_file: by-nc-nd.png + icon_file: images/by-nc-nd.png url: https://creativecommons.org/licenses/by-nc-nd/4.0/ - title: Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) short_label: CC BY-NC-SA 4.0 - icon_file: by-nc-sa.png + icon_file: images/by-nc-sa.png url: https://creativecommons.org/licenses/by-nc-sa/4.0/ diff --git a/media_attribution.module b/media_attribution.module index e0e1f78..db6b631 100644 --- a/media_attribution.module +++ b/media_attribution.module @@ -13,6 +13,7 @@ use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\taxonomy\Entity\Term; use Drupal\file\Entity\File; use Drupal\Component\Serialization\Yaml; +use Drupal\media_attribution\LicenseLoader; /** * Implements hook_help(). @@ -149,34 +150,8 @@ function media_attribution_load_default_licenses() { $license_data = Yaml::decode($file_contents); foreach ($license_data as $license_item) { - media_attribution_create_license_term($license_item['title'],$license_item['short_label'], $license_item['icon_file'], $license_item['url']); + $icon_file_path = drupal_get_path('module', 'media_attribution') . "/" . $license_item['icon_file']; + LicenseLoader::createLicenseTerm($license_item['title'],$license_item['short_label'], $icon_file_path, $license_item['url']); } } -function media_attribution_create_license_term($term_title, $term_short_label, $icon_filename, $license_url) { - $icon_file_uri = drupal_get_path('module', 'media_attribution') . "/images/$icon_filename"; - - // Just in case the file has already been created. - $icon_files = \Drupal::entityTypeManager() - ->getStorage('file') - ->loadByProperties(['uri' => $icon_file_uri]); - $icon_file = reset($icon_files); - // if not create a file - if (!$icon_file) { - $icon_file = File::create([ - 'uri' => $icon_file_uri, - ]); - $icon_file->save(); - } - - $tid = Term::create([ - 'name' => $term_title, - 'vid' => 'media_attribution_licenses', - '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(); -} diff --git a/src/Commands/MediaAttributionCommands.php b/src/Commands/MediaAttributionCommands.php new file mode 100644 index 0000000..8f3d29a --- /dev/null +++ b/src/Commands/MediaAttributionCommands.php @@ -0,0 +1,46 @@ +getConfig(); + $config->get('cwd'); + $this->output()->writeln("Loading licenses from $file."); + $cwd = $this->config->get('env')['cwd']; + $full_path = $cwd . '/' . $file; + $file_contents = file_get_contents($full_path); + $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'] : ''; + LicenseLoader::createLicenseTerm($license_item['title'],$license_item['short_label'], $icon_file_path, $license_item['url']); + } + + } + +} \ No newline at end of file diff --git a/src/LicenseLoader.php b/src/LicenseLoader.php new file mode 100644 index 0000000..1cd7a45 --- /dev/null +++ b/src/LicenseLoader.php @@ -0,0 +1,69 @@ +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(); + } + $tid = Term::create([ + 'name' => $term_title, + 'vid' => 'media_attribution_licenses', + '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 { + // Create the term without an image. + $tid = Term::create([ + 'name' => $term_title, + 'vid' => 'media_attribution_licenses', + 'field_license_link' => ['title' => $term_short_label, 'uri' => $license_url], + ])->save(); + } + + return $tid; + } +} \ No newline at end of file