Alexander O'Neill 5 years ago
parent
commit
5812d011a6
  1. 2
      src/Commands/MediaAttributionCommands.php
  2. 107
      src/LicenseLoader.php

2
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']);
}
}

107
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,24 +27,36 @@ 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,
'vid' => 'media_attribution_licenses',
@ -54,8 +67,7 @@ class LicenseLoader {
'title' => $term_title,
]
])->save();
}
else {
} else {
// Create the term without an image.
$tid = Term::create([
'name' => $term_title,
@ -66,4 +78,61 @@ class LicenseLoader {
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;
}
}
Loading…
Cancel
Save