Compare commits

...

14 Commits
wip ... 8.x-1.x

  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,47 +1,40 @@
# Installation
## Patch requirement
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:
Use composer to download the module,
When you install the module, it creates the following configurations:
* Taxonomy vocabulary
* Licences (media_attribution_licenses) - Includes License Link and License Icon fields
* The vocabulary is populated with a set of Creative Commons licenses.
* Paragraphs type
* License Attribution (license_attribution)
* This field is automatically added to the Image media type on install.
# Useage
# Use
The installation script automatically adds the attribution paragraphs field
to the Image media type if it exists. Otherwise adding attribution
can be done at Admin -> Structure -> Media Types.
The installation script automatically adds teh attribution paragraphs field
to the Image media type if it exists. This is installed by default
by the Lightning distribution. Otherwise adding attribution
can be one at Admin -> Structure -> Media Types.
In CKEditor, when you click the Embed Media button, the attribution links
fields will show up in the Media along with Title, Alt text, etc.
Alternatively you can edit the media content entity directly.
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.
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.,
"All rights reserved, used with permisssion."
This source and author info will be rendered below an embedded media object
when showing the node, following teh format preferred by Creative Commons,
when showing the node, following the format preferred by Creative Commons,
outlined at Best Practices for Attribution:
https://wiki.creativecommons.org/wiki/best_practices_for_attribution
@ -50,7 +43,7 @@ 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.
edited, and other licenses can be added as needed.
## Bulk-loading Licenses via Drush
@ -58,7 +51,7 @@ Run `drush help media_attribution:load_licenses` for how to load license data fr
# Authors
Alexander O'Neill ([alxp](https://drupal.org/u/alxp))
Alexander O'Neill https://drupal.org/u/alxp
# Sponsoring Organization

10
composer.json

@ -1,11 +1,11 @@
{
"name": "drupal/media_attribution",
"name": "roblib/media_attribution",
"type": "drupal-module",
"description": "Attach an attribution and license info to embedded media",
"keywords": [
"Drupal"
],
"license": "GPL-2.0+",
"license": "GPL-2.0-or-later",
"homepage": "https://www.drupal.org/project/media_attribution",
"minimum-stability": "dev",
"support": {
@ -17,11 +17,13 @@
"Drupal\\media_attribution\\": "src/"
}
},
"require": {},
"require": {
"drupal/paragraphs": ">=1.15"
},
"extra": {
"drush": {
"services": {
"drush.services.yml": "^9"
"drush.services.yml": ">=9"
}
}
}

3
media_attribution.info.yml

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

45
media_attribution.module

@ -59,6 +59,7 @@ function media_attribution_modules_installed($modules) {
'collapse_edit_all' => 'collapse_edit_all',
]
],
'weight' => 35,
'type' => 'paragraphs',
])
->save();
@ -70,7 +71,7 @@ function media_attribution_modules_installed($modules) {
$view_entity->setComponent('field_image_attribution', [
'type' => 'entity_reference_revisions_entity_view',
'label' => 'hidden',
'settings' => ['view_mode' => 'default', 'link' => ''],
'settings' => ['view_mode' => 'license_attribution', 'link' => ''],
])->save();
}
}
@ -78,6 +79,21 @@ function media_attribution_modules_installed($modules) {
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().
*
@ -89,12 +105,13 @@ function media_attribution_preprocess_entity_embed_container(&$variables) {
&& $attributions = $variables['element']['#entity']->get('field_image_attribution')
->referencedEntities()) {
$attribution = $attributions[0];
$author = $attribution->get('field_attribution_author')->getValue()[0];
$author_field = $attribution->get('field_attribution_author')->getValue();
$author = isset($author_field[0]) ? $author_field[0] : '';
$source = $attribution->get('field_attribution_source')->getValue()[0];
$source = $attribution->get('field_attribution_source')->count() > 0 ? $attribution->get('field_attribution_source')->getValue()[0] : '';
$license = $attribution->get('field_license')->entity;
$license_link = !empty($license) ? $license->get('field_license_link')->getValue()[0] : FALSE;
$attribution_note = $attribution->get('field_attribution_note')->getValue()[0];
$attribution_note = $attribution->get('field_attribution_note')->count() > 0 ? $attribution->get('field_attribution_note')->getValue()[0]: '';
$attribution_text = [];
if (!empty($source['uri'])) {
$attribution_text['source'] = [
@ -132,10 +149,11 @@ function media_attribution_preprocess_entity_embed_container(&$variables) {
'#prefix' => ' ',
];
}
$children = ['#markup' => $variables['children']];
$variables['children'] = [
'first' => [
'#type' => 'markup',
'#markup' => render($variables['children'])
'#markup' => \Drupal::service('renderer')->render($children)
],
$attribution_text
];
@ -145,13 +163,26 @@ function media_attribution_preprocess_entity_embed_container(&$variables) {
* Read the list of pre-defined Creative Commons media_attribution_licenses and create corresponding taxonomy terms.
*/
function media_attribution_load_default_licenses() {
$file_path = drupal_get_path('module', 'media_attribution') . '/media_attribution.default.licenses.yml';
$file_path = \Drupal::service('extension.list.module')->getPath('media_attribution') . '/media_attribution.default.licenses.yml';
$file_contents = file_get_contents($file_path);
$license_data = Yaml::decode($file_contents);
foreach ($license_data as $license_item) {
$icon_file_path = drupal_get_path('module', 'media_attribution') . "/" . $license_item['icon_file'];
$icon_file_path = \Drupal::service('extension.list.module')->getPath('media_attribution') . "/" . $license_item['icon_file'];
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,7 +37,10 @@ class MediaAttributionCommands extends DrushCommands {
$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'] : '';
$icon_file_path = '';
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']);
}

18
src/LicenseLoader.php

@ -7,8 +7,7 @@ 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.
@ -27,12 +26,12 @@ 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)
{
public static function createOrUpdateLicenseTerm($term_title, $term_short_label, $icon_file_path, $license_url) {
$tids = array_values(\Drupal::entityQuery('taxonomy_term')
->accessCheck(TRUE)
->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];
@ -125,9 +124,16 @@ class LicenseLoader
// if not create a 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');
$icon_uri = $fs->copy($icon_file_path, 'public://');
}
$icon_file = File::create([
'uri' => $icon_uri,
]);

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

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

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

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

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

@ -0,0 +1,2 @@
&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