webdam-1.0.x-dev/webdam.module
webdam.module
<?php
/**
* @file
* Provides webdam integration.
*/
use Drupal\webdam\Plugin\Field\FieldType\WebdamMetadataItem;
use Drupal\webdam\Plugin\media\Source\Webdam;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\editor\Entity\Editor;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\media\Entity\MediaType;
use Drupal\media\MediaInterface;
use Drupal\media\MediaTypeInterface;
/**
* Implements hook_help().
*/
function webdam_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.webdam':
$output = '';
$output .= '<h3>' . t('Webdam') . '</h3>';
$output .= '<p>' . t('WebDAM is an online image bank and Digital Asset Management solution that allows you to store, manage and share your media. Using the DAM API you can access assets, collections, titles and various other metadata fields as well as pointers to the file destinations.') . '</p>';
$output .= '<p>' . t('WebDAM uses OAuth version 2 with header authentication to provide authorized access to its API.') . '</p>';
$output .= '<p>' . t('The API endpoint URLs are set up as: https://apiv2.webdamdb.com/[method].') . '</p>';
$output .= '<p>' . t('The API documentation is available on <a href=":url">https://www.damsuccess.com/hc/en-us/articles/202134055-REST-API</a>', [
':url' => 'https://www.damsuccess.com/hc/en-us/articles/202134055-REST-API',
]) . '</p>';
$output .= '<p>' . t('To request API access contact support.') . '</p>';
return $output;
case 'webdam.configuration_form':
$output = '';
$output .= '<p>' . t('To enable OAuth based access for Webdam API fill in provided fields.');
$output .= ' ' . t('Use the provided username and password for your WebDAM user account. To accquire api access (Client ID, Client secret) contact support.') . '</p>';
return $output;
}
}
/**
* Implements hook_theme().
*/
function webdam_theme($existing, $type, $theme, $path) {
return [
'webdam_video' => [
'variables' => [
'attributes' => [],
'source_attributes' => [],
],
],
];
}
/**
* Implements hook_form_alter().
*/
function webdam_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ('media_webdam_edit_form' == $form_id) {
/** @var \Drupal\media\MediaInterface $entity */
$entity = $form_state->getBuildInfo()['callback_object']->getEntity();
if ($entity->getSource() instanceof Webdam && $url = webdam_media_url($entity)) {
$form['edit_on_webdam'] = [
'#type' => 'inline_template',
'#template' => 'It is strongly advised to <a href="{{ uri }}" target="_blank"><i>edit asset\'s metadata on Webdam.</i></a> Please note that the changes won\'t be automatically updated here.',
'#context' => [
'uri' => $url->toString(),
],
];
}
}
}
/**
* Returns edit link for Webdam asset.
*
* @param \Drupal\media\MediaInterface $media
* The media entity with webdam as type provider.
*
* @return bool|\Drupal\Core\Url
* Returns edit link for Webdam asset or FALSE.
*/
function webdam_media_url(MediaInterface $media) {
$source_plugin = $media->getSource();
if (!$source_plugin instanceof Webdam) {
return FALSE;
}
if (!$account_domain = \Drupal::config('webdam.settings')->get('account_domain')) {
return FALSE;
}
return Url::fromUri($account_domain . '/cloud/', [
'fragment' => 'asset/' . $source_plugin->getSourceFieldValue($media),
]);
}
/**
* Implements hook_entity_extra_field_info().
*/
function webdam_entity_extra_field_info() {
$extra = [];
foreach (MediaType::loadMultiple() as $type) {
/** @var \Drupal\media\Entity\MediaType $type */
if ($type->getSource() instanceof Webdam) {
$extra['media'][$type->id()]['form']['edit_on_webdam'] = [
'label' => t('Edit on Webdam'),
'description' => t('Displays message that advises to edit metadata on Webdam.'),
'weight' => -1,
'visible' => TRUE,
];
}
}
return $extra;
}
/**
* Implements hook_page_top().
*/
function webdam_page_top(array &$page_top) {
// Hide toolbar on batches that are happening inside Entity browser.
if (!empty($page_top['toolbar']) && ($batch = batch_get()) && !empty($batch['sets'][0]['finished'][0]) && $batch['sets'][0]['finished'][0] == 'Drupal\webdam\Plugin\EntityBrowser\Widget\WebdamUpload') {
$page_top['toolbar']['#access'] = FALSE;
}
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function webdam_media_type_insert(MediaTypeInterface $media_type) {
// Do not alter configuration during config synch.
if ($media_type->isSyncing()) {
return;
}
// Create a metadata field on new Webdam media types.
if ($media_type->getSource() instanceof Webdam) {
/** @var \Drupal\webdam\Plugin\media\Source\Webdam $source */
$source = $media_type->getSource();
$metadata_field_storage = FieldStorageConfig::loadByName('media', WebdamMetadataItem::METADATA_FIELD_NAME);
if (!$metadata_field_storage) {
$metadata_field_storage = $source->createMetadataFieldStorage();
$metadata_field_storage->save();
}
$metadata_field = $source->createMetadataField($media_type);
$metadata_field->save();
$transformations_field_storage = FieldStorageConfig::loadByName('media', Webdam::TRANSFORMATIONS_FIELD_NAME);
if (!$transformations_field_storage) {
$transformations_field_storage = $source->createTransformationsFieldStorage();
$transformations_field_storage->save();
}
$transformations_field = $source->createTransformationsField($media_type);
$transformations_field->save();
}
}
