Alexander O'Neill
5 years ago
7 changed files with 157 additions and 36 deletions
@ -0,0 +1,5 @@ |
|||||||
|
services: |
||||||
|
media_attribution.commands: |
||||||
|
class: \Drupal\media_attribution\Commands\MediaAttributionCommands |
||||||
|
tags: |
||||||
|
- { name: drush.command } |
@ -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']); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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…
Reference in new issue