' . t('About') . ''; $output .= '
' . t('Attach an attribution and license info to embedded media') . '
'; return $output; default: } } /** * Implements hook_modules_installed(). * * Add attribution form to media embed forms and view displays. */ function media_attribution_modules_installed($modules) { foreach (['default', 'media_browser'] as $form_variant) { $form_entity = EntityFormDisplay::load("media.image.$form_variant"); if (!empty($form_entity)) { $form_entity->setComponent('field_image_attribution', [ 'region' => 'content', 'settings' => [ 'title' => 'Paragraph', 'title_plural' => 'Paragraphs', 'edit_mode' => 'open', 'closed_mode' => 'summary', 'autocollapse' => 'none', 'closed_mode_threshold' => 0, 'add_mode' => 'dropdown', 'form_display_mode' => 'default', 'default_paragraph_type' => '', 'features' => [ 'duplicate' => 'duplicate', 'collapse_edit_all' => 'collapse_edit_all', ] ], 'weight' => 35, 'type' => 'paragraphs', ]) ->save(); } } foreach (['default', 'embedded'] as $entity_view_variant) { $view_entity = EntityViewDisplay::load("media.image.$entity_view_variant"); if (!empty($view_entity)) { $view_entity->setComponent('field_image_attribution', [ 'type' => 'entity_reference_revisions_entity_view', 'label' => 'hidden', 'settings' => ['view_mode' => 'license_attribution', 'link' => ''], ])->save(); } } // Load default license terms from the module config. 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 = $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(). * * @param $variables */ function media_attribution_preprocess_entity_embed_container(&$variables) { if ($variables['element']['#entity']->hasField('field_image_attribution') && $attributions = $variables['element']['#entity']->get('field_image_attribution') ->referencedEntities()) { $attribution = $attributions[0]; $author = $attribution->get('field_attribution_author')->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')->count() > 0 ? $attribution->get('field_attribution_note')->getValue()[0]: ''; $attribution_text = []; if (!empty($source['uri'])) { $attribution_text['source'] = [ '#type' => 'link', '#title' => $source['title'], '#prefix' => "