Compare commits

..

No commits in common. '8.x-1.x' and 'wip' have entirely different histories.
8.x-1.x ... wip

  1. 45
      README.md
  2. 10
      composer.json
  3. 3
      media_attribution.info.yml
  4. 45
      media_attribution.module
  5. 5
      src/Commands/MediaAttributionCommands.php
  6. 18
      src/LicenseLoader.php
  7. 1
      templates/field--paragraph--field-attribution-author--license-attribution.html.twig
  8. 1
      templates/field--paragraph--field-attribution-source--license-attribution.html.twig
  9. 2
      templates/paragraph--license-attribution--license-attribution.html.twig

45
README.md

@ -1,40 +1,47 @@
# Installation # Installation
Use composer to download the module, ## Patch requirement
When you install the module, it creates the following configurations:
In order to properly use the license attribution form items with the inline
media browser CKEditor button (the 'star' button), the following patch is required:
https://www.drupal.org/files/issues/paragraphs-missing-langcode-2901390-9.patch
From this Paragraphs issue: Integrity constraint violation: 1048 Column 'langcode' cannot be null
https://www.drupal.org/project/paragraphs/issues/2901390
## Enabling the module
Install as you normally would. This module creates the following entity types:
* Taxonomy vocabulary * Taxonomy vocabulary
* Licences (media_attribution_licenses) - Includes License Link and License Icon fields * Licences (media_attribution_licenses) - Includes License Link and License Icon fields
* The vocabulary is populated with a set of Creative Commons licenses.
* Paragraphs type * Paragraphs type
* License Attribution (license_attribution) * License Attribution (license_attribution)
* This field is automatically added to the Image media type on install.
# Useage
The installation script automatically adds the attribution paragraphs field # Use
to the Image media type if it exists. Otherwise adding attribution
can be done at Admin -> Structure -> Media Types.
In CKEditor, when you click the Embed Media button, the attribution links The installation script automatically adds teh attribution paragraphs field
fields will show up in the Media along with Title, Alt text, etc. to the Image media type if it exists. This is installed by default
Alternatively you can edit the media content entity directly. by the Lightning distribution. Otherwise adding attribution
can be one at Admin -> Structure -> Media Types.
The attribution paragraph field has the following components The attribution paragraph field has the following components
* Source Work link, including a title and URL. This title is Source Work link, including a title and URL. This title is
usually the title of the original work. usually the title of the original work.
* Original Author link, including name and URL of the author's home page. Original Author link, including name and URL of the author's home page.
* License type - optional, the license under which the linked work is released. License type - optional, the license under which the linked work is released.
* Free-form attribution text - optional, arbitrary text to clarify attribution. If no Free-form attribution text - optional, arbitrary text to clarify attribution. If no
license is selected, this text can be used to identify copyrighted works with e.g., license is selected, this text can be used to identify copyrighted works with e.g.,
"All rights reserved, used with permisssion." "All rights reserved, used with permisssion."
This source and author info will be rendered below an embedded media object This source and author info will be rendered below an embedded media object
when showing the node, following the format preferred by Creative Commons, when showing the node, following teh format preferred by Creative Commons,
outlined at Best Practices for Attribution: outlined at Best Practices for Attribution:
https://wiki.creativecommons.org/wiki/best_practices_for_attribution https://wiki.creativecommons.org/wiki/best_practices_for_attribution
@ -43,7 +50,7 @@ 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 licenses can be added as needed. edited, and other license types can be added as needed.
## Bulk-loading Licenses via Drush ## Bulk-loading Licenses via Drush
@ -51,7 +58,7 @@ Run `drush help media_attribution:load_licenses` for how to load license data fr
# Authors # Authors
Alexander O'Neill https://drupal.org/u/alxp Alexander O'Neill ([alxp](https://drupal.org/u/alxp))
# Sponsoring Organization # Sponsoring Organization

10
composer.json

@ -1,11 +1,11 @@
{ {
"name": "roblib/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": [ "keywords": [
"Drupal" "Drupal"
], ],
"license": "GPL-2.0-or-later", "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",
"support": { "support": {
@ -17,13 +17,11 @@
"Drupal\\media_attribution\\": "src/" "Drupal\\media_attribution\\": "src/"
} }
}, },
"require": { "require": {},
"drupal/paragraphs": ">=1.15"
},
"extra": { "extra": {
"drush": { "drush": {
"services": { "services": {
"drush.services.yml": ">=9" "drush.services.yml": "^9"
} }
} }
} }

3
media_attribution.info.yml

@ -1,9 +1,8 @@
name: 'Media Attribution' name: 'Media Attribution'
type: module type: module
description: 'Attach attribution and license info to media entities.' description: 'Attach attribution and license info to media entities.'
core_version_requirement: ^9.3 || ^10 core: 8.x
package: 'media' package: 'media'
dependencies: dependencies:
- drupal:media - drupal:media
- drupal:media_library
- paragraphs:paragraphs - paragraphs:paragraphs

45
media_attribution.module

@ -59,7 +59,6 @@ function media_attribution_modules_installed($modules) {
'collapse_edit_all' => 'collapse_edit_all', 'collapse_edit_all' => 'collapse_edit_all',
] ]
], ],
'weight' => 35,
'type' => 'paragraphs', 'type' => 'paragraphs',
]) ])
->save(); ->save();
@ -71,7 +70,7 @@ function media_attribution_modules_installed($modules) {
$view_entity->setComponent('field_image_attribution', [ $view_entity->setComponent('field_image_attribution', [
'type' => 'entity_reference_revisions_entity_view', 'type' => 'entity_reference_revisions_entity_view',
'label' => 'hidden', 'label' => 'hidden',
'settings' => ['view_mode' => 'license_attribution', 'link' => ''], 'settings' => ['view_mode' => 'default', 'link' => ''],
])->save(); ])->save();
} }
} }
@ -79,21 +78,6 @@ function media_attribution_modules_installed($modules) {
media_attribution_load_default_licenses(); media_attribution_load_default_licenses();
} }
function media_attribution_preprocess_field__paragraph__field_attribution_author__license_attribution(&$variables) {
$variables['source_prefix'] = \Drupal::translation()->translate('by');
}
function media_attribution_preprocess_paragraph__license_attribution__license_attribution(&$variables) {
$variables['license_prefix'] = \Drupal::translation()->translate('Licensed under');
$items = isset($variables['elements']['field_license']['#items']) ? $variables['elements']['field_license']['#items'] : [];
if (!empty($items)) {
$referencedEntity = $variables['elements']['field_license']['#items']
->first()->get('entity')->getTarget()->getValue();
$variables['license_link'] = $referencedEntity->get('field_license_link')->first()->view();
}
}
/** /**
* Implementation of hook_preprocess_entity_embed_container(). * Implementation of hook_preprocess_entity_embed_container().
* *
@ -105,13 +89,12 @@ function media_attribution_preprocess_entity_embed_container(&$variables) {
&& $attributions = $variables['element']['#entity']->get('field_image_attribution') && $attributions = $variables['element']['#entity']->get('field_image_attribution')
->referencedEntities()) { ->referencedEntities()) {
$attribution = $attributions[0]; $attribution = $attributions[0];
$author_field = $attribution->get('field_attribution_author')->getValue(); $author = $attribution->get('field_attribution_author')->getValue()[0];
$author = isset($author_field[0]) ? $author_field[0] : '';
$source = $attribution->get('field_attribution_source')->count() > 0 ? $attribution->get('field_attribution_source')->getValue()[0] : ''; $source = $attribution->get('field_attribution_source')->getValue()[0];
$license = $attribution->get('field_license')->entity; $license = $attribution->get('field_license')->entity;
$license_link = !empty($license) ? $license->get('field_license_link')->getValue()[0] : FALSE; $license_link = !empty($license) ? $license->get('field_license_link')->getValue()[0] : FALSE;
$attribution_note = $attribution->get('field_attribution_note')->count() > 0 ? $attribution->get('field_attribution_note')->getValue()[0]: ''; $attribution_note = $attribution->get('field_attribution_note')->getValue()[0];
$attribution_text = []; $attribution_text = [];
if (!empty($source['uri'])) { if (!empty($source['uri'])) {
$attribution_text['source'] = [ $attribution_text['source'] = [
@ -149,11 +132,10 @@ function media_attribution_preprocess_entity_embed_container(&$variables) {
'#prefix' => ' ', '#prefix' => ' ',
]; ];
} }
$children = ['#markup' => $variables['children']];
$variables['children'] = [ $variables['children'] = [
'first' => [ 'first' => [
'#type' => 'markup', '#type' => 'markup',
'#markup' => \Drupal::service('renderer')->render($children) '#markup' => render($variables['children'])
], ],
$attribution_text $attribution_text
]; ];
@ -163,26 +145,13 @@ function media_attribution_preprocess_entity_embed_container(&$variables) {
* Read the list of pre-defined Creative Commons media_attribution_licenses and create corresponding taxonomy terms. * Read the list of pre-defined Creative Commons media_attribution_licenses and create corresponding taxonomy terms.
*/ */
function media_attribution_load_default_licenses() { function media_attribution_load_default_licenses() {
$file_path = \Drupal::service('extension.list.module')->getPath('media_attribution') . '/media_attribution.default.licenses.yml'; $file_path = drupal_get_path('module', 'media_attribution') . '/media_attribution.default.licenses.yml';
$file_contents = file_get_contents($file_path); $file_contents = file_get_contents($file_path);
$license_data = Yaml::decode($file_contents); $license_data = Yaml::decode($file_contents);
foreach ($license_data as $license_item) { foreach ($license_data as $license_item) {
$icon_file_path = \Drupal::service('extension.list.module')->getPath('media_attribution') . "/" . $license_item['icon_file']; $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']); LicenseLoader::createLicenseTerm($license_item['title'],$license_item['short_label'], $icon_file_path, $license_item['url']);
} }
} }
function media_attribution_theme() {
return [
'paragraph__license_attribution__license_attribution' => [
'base hook' => 'paragraph',
],
'field__paragraph__field_attribution_source__license_attribution' => [
'base hook' => 'field',
],
'field__paragraph__field_attribution_author__license_attribution' => [
'base hook' => 'field',
],
];
}

5
src/Commands/MediaAttributionCommands.php

@ -37,10 +37,7 @@ class MediaAttributionCommands extends DrushCommands {
$license_data = Yaml::decode($file_contents); $license_data = Yaml::decode($file_contents);
foreach ($license_data as $license_item) { foreach ($license_data as $license_item) {
$icon_file_path = ''; $icon_file_path = isset($license_item['icon_file']) ? $cwd . '/' . $license_item['icon_file'] : '';
if (!empty($license_item['icon_file'])) {
$icon_file_path = substr($license_item['icon_file'], 0, 1) == '/' ? $license_item['icon_file'] : $cwd . '/' . $license_item['icon_file'];
}
LicenseLoader::createOrUpdateLicenseTerm($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']);
} }

18
src/LicenseLoader.php

@ -7,7 +7,8 @@ use Drupal\taxonomy\Entity\Term;
use Drupal\file\Entity\File; use Drupal\file\Entity\File;
use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\Integer;
class LicenseLoader { class LicenseLoader
{
/** /**
* Create a new license term with the given values. * Create a new license term with the given values.
@ -26,12 +27,12 @@ class LicenseLoader {
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException * @throws \Drupal\Core\Entity\EntityStorageException
*/ */
public static function createOrUpdateLicenseTerm($term_title, $term_short_label, $icon_file_path, $license_url) { public static function createOrUpdateLicenseTerm($term_title, $term_short_label, $icon_file_path, $license_url)
{
$tids = array_values(\Drupal::entityQuery('taxonomy_term') $tids = array_values(\Drupal::entityQuery('taxonomy_term')
->accessCheck(TRUE)
->condition('name', $term_title) ->condition('name', $term_title)
->execute()); ->execute());
print_r($tids);
if ($tids) { if ($tids) {
self::updateLicenseTerm($tids[0], $term_title, $term_short_label, $icon_file_path, $license_url); self::updateLicenseTerm($tids[0], $term_title, $term_short_label, $icon_file_path, $license_url);
return $tids[0]; return $tids[0];
@ -124,16 +125,9 @@ class LicenseLoader {
// if not create a file // if not create a file
if (!$icon_file) { if (!$icon_file) {
if (substr($icon_file_path, 0, strlen(DRUPAL_ROOT)) == DRUPAL_ROOT) {
$icon_uri = substr($icon_file_path, strlen(DRUPAL_ROOT), strlen($icon_file_path));
}
elseif (substr($icon_file_path, 0, 1) != '/') {
$icon_uri = $icon_file_path;
}
else {
$fs = \Drupal::service('file_system'); $fs = \Drupal::service('file_system');
$icon_uri = $fs->copy($icon_file_path, 'public://'); $icon_uri = $fs->copy($icon_file_path, 'public://');
}
$icon_file = File::create([ $icon_file = File::create([
'uri' => $icon_uri, 'uri' => $icon_uri,
]); ]);

1
templates/field--paragraph--field-attribution-author--license-attribution.html.twig

@ -1 +0,0 @@
{{ source_prefix }} {{ items.0 }}

1
templates/field--paragraph--field-attribution-source--license-attribution.html.twig

@ -1 +0,0 @@
{{ items.0 }}

2
templates/paragraph--license-attribution--license-attribution.html.twig

@ -1,2 +0,0 @@
&ldquo;<cite>{{ elements.field_attribution_source.0 }}</cite>&rdquo; {{ elements.field_attribution_author }}.
<span class="license-prefix"> {{ license_prefix }} {{ license_link }}</span>
Loading…
Cancel
Save