Browse Source

Add Drush command to bulk load licenses.

wip 8.x-1.0-beta6
Alexander O'Neill 5 years ago
parent
commit
d4b1864b71
  1. 12
      README.md
  2. 18
      composer.json
  3. 5
      drush.services.yml
  4. 12
      media_attribution.default.licenses.yml
  5. 31
      media_attribution.module
  6. 46
      src/Commands/MediaAttributionCommands.php
  7. 69
      src/LicenseLoader.php

12
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 Installing the module creates License taxonomy term entries for all of the use
variations of the international Creative Commons licenses. These can be variations of the international Creative Commons licenses. These can be
edited, and other license types can be added as needed. 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/)

18
composer.json

@ -2,7 +2,9 @@
"name": "drupal/media_attribution", "name": "drupal/media_attribution",
"type": "drupal-module", "type": "drupal-module",
"description": "Attach an attribution and license info to embedded media", "description": "Attach an attribution and license info to embedded media",
"keywords": ["Drupal"], "keywords": [
"Drupal"
],
"license": "GPL-2.0+", "license": "GPL-2.0+",
"homepage": "https://www.drupal.org/project/media_attribution", "homepage": "https://www.drupal.org/project/media_attribution",
"minimum-stability": "dev", "minimum-stability": "dev",
@ -10,5 +12,17 @@
"issues": "https://www.drupal.org/project/issues/media_attribution", "issues": "https://www.drupal.org/project/issues/media_attribution",
"source": "http://cgit.drupalcode.org/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"
}
}
}
} }

5
drush.services.yml

@ -0,0 +1,5 @@
services:
media_attribution.commands:
class: \Drupal\media_attribution\Commands\MediaAttributionCommands
tags:
- { name: drush.command }

12
media_attribution.default.licenses.yml

@ -2,30 +2,30 @@
- -
title: Attribution 4.0 International (CC BY 4.0) title: Attribution 4.0 International (CC BY 4.0)
short_label: 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/ url: https://creativecommons.org/licenses/by/4.0/
- -
title: Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) title: Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
short_label: 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/ url: https://creativecommons.org/licenses/by-nc/4.0/
- -
title: Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0) title: Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0)
short_label: 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/ url: https://creativecommons.org/licenses/by-nd/4.0/
- -
title: Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) title: Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
short_label: 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/ url: https://creativecommons.org/licenses/by-sa/4.0/
- -
title: Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) title: Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)
short_label: 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/ url: https://creativecommons.org/licenses/by-nc-nd/4.0/
- -
title: Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) title: Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
short_label: 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/ url: https://creativecommons.org/licenses/by-nc-sa/4.0/

31
media_attribution.module

@ -13,6 +13,7 @@ use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Term;
use Drupal\file\Entity\File; use Drupal\file\Entity\File;
use Drupal\Component\Serialization\Yaml; use Drupal\Component\Serialization\Yaml;
use Drupal\media_attribution\LicenseLoader;
/** /**
* Implements hook_help(). * Implements hook_help().
@ -149,34 +150,8 @@ function media_attribution_load_default_licenses() {
$license_data = Yaml::decode($file_contents); $license_data = Yaml::decode($file_contents);
foreach ($license_data as $license_item) { 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();
}

46
src/Commands/MediaAttributionCommands.php

@ -0,0 +1,46 @@
<?php
namespace Drupal\media_attribution\Commands;
use Drush\Commands\DrushCommands;
use Drupal\Component\Serialization\Yaml;
use Drupal\media_attribution\LicenseLoader;
class MediaAttributionCommands extends DrushCommands {
/**
* Loads licenses from a YAML file. The format is:
*
* -
* title:
* short_label:
* url:
* icon_file:
*
* @param $file
* YAML file containing licenses to load.
*
* @command media_attribution:load_licenses
* @aliases ma-load
*
* @usage media_attribution:load_licenses licenses_file.yml
* Load the contents of the licenses file as new terms in the Media Attribution Licenses vocabulary.
*/
public function loadLicenses($file) {
$config = $this->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']);
}
}
}

69
src/LicenseLoader.php

@ -0,0 +1,69 @@
<?php
namespace Drupal\media_attribution;
use Drupal\Core\File\FileSystemInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\file\Entity\File;
use phpDocumentor\Reflection\Types\Integer;
class LicenseLoader {
/**
* Create a new license term with the given values.
*
* @param $term_title
* Long-form title.
* @param $term_short_label
* Short-form title
* @param $icon_file_path
* Icon file path.
* @param $license_url
* URL for the license home page.
* @return int
* The new term id.
* @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();
}
$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;
}
}
Loading…
Cancel
Save