bynder.module in Bynder 8.2
Same filename and directory in other branches
Provides bynder integration.
File
bynder.moduleView source
<?php
/**
* @file
* Provides bynder integration.
*/
use Drupal\bynder\Plugin\Field\FieldType\BynderMetadataItem;
use Drupal\bynder\Plugin\media\Source\Bynder;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\media\Entity\MediaType;
use Drupal\media\MediaInterface;
use Drupal\media\MediaTypeInterface;
/**
* Implements hook_help().
*/
function bynder_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.bynder':
$output = '';
$output .= '<h3>' . t('Bynder') . '</h3>';
$output .= '<p>' . t('Bynder 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 media, brands, categories, titles, copyright values and various other meta data fields as well as pointers to the file destinations.') . '</p>';
$output .= '<p>' . t('Bynder uses OAth version 1 with header authentication to provide authorize access to its API.') . '</p>';
$output .= '<p>' . t('The API endpoint URLs are setup as: http://[accountdomain]/api/[version]/[method].') . '</p>';
$output .= '<p>' . t('To request access contact <a href=":support">support</a>.', [
':support' => 'mailto:support@getbynder.com',
]) . '</p>';
$output .= '<p>' . t('To get a customer key and secret <a href=":guide">follow the official guide</a> and to acquire an access token and secred take <a href=":api">a look at the API documentation</a>.', [
':guide' => 'https://support.getbynder.com/hc/en-us/articles/208734785',
':api' => 'http://docs.bynder.apiary.io/#reference/token',
]) . '</p>';
$output .= '<p>' . t('API documentation is available at <a href=":url">:url</a>.', [
':url' => 'http://docs.bynder.apiary.io/',
]) . '</p>';
return $output;
case 'bynder.configuration_form':
$output = '';
$output .= '<p>' . t('To enable OAuth based access for Bynder API fill in provided fields.');
$output .= ' ' . t('To get a customer key and secret <a href=":guide">follow the official guide</a> and to acquire an access token and secred take <a href=":api">a look at the API documentation</a>.', [
':guide' => 'https://support.getbynder.com/hc/en-us/articles/208734785',
':api' => 'http://docs.bynder.apiary.io/#reference/token',
]) . '</p>';
return $output;
}
}
/**
* Implements hook_theme().
*/
function bynder_theme($existing, $type, $theme, $path) {
return [
'bynder_search_item' => [
'variables' => [
'thumbnail_uri' => '',
'name' => t('N/A'),
'type' => '',
],
],
];
}
/**
* Implements hook_form_alter().
*/
function bynder_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ('media_bynder_edit_form' == $form_id) {
/** @var \Drupal\media\MediaInterface $entity */
$entity = $form_state
->getBuildInfo()['callback_object']
->getEntity();
if ($entity
->getSource() instanceof Bynder) {
$form['edit_on_bynder'] = [
'#type' => 'inline_template',
'#template' => 'It is strongly advised to <a href="{{ uri }}" target="_blank"><i>edit asset\'s metadata on Bynder.</i></a> Please note that the changes won\'t be automatically updated here.',
'#context' => [
'uri' => get_uri_to_bynder($entity),
],
];
}
}
}
/**
* Returns edit link for Bynder asset.
*
* @param \Drupal\media\MediaInterface $media
* The media entity with bynder as type provider.
*
* @return bool|string
* Returns edit link for Bynder asset or FALSE.
*/
function get_uri_to_bynder(MediaInterface $media) {
$source_plugin = $media
->getSource();
if (!$source_plugin instanceof Bynder) {
return FALSE;
}
if (!($account_domain = \Drupal::config('bynder.settings')
->get('account_domain'))) {
return FALSE;
}
return Url::fromUri($account_domain . '/media', [
'query' => [
'mediaId' => $source_plugin
->getSourceFieldValue($media),
],
])
->toString();
}
/**
* Implements hook_entity_extra_field_info().
*/
function bynder_entity_extra_field_info() {
$extra = [];
foreach (MediaType::loadMultiple() as $type) {
/** @var \Drupal\media\Entity\MediaType $type */
if ($type
->getSource() instanceof Bynder) {
$extra['media'][$type
->id()]['form']['edit_on_bynder'] = [
'label' => t('Edit on Bynder'),
'description' => t('Displays message that advises to edit metadata on Bynder.'),
'weight' => -1,
'visible' => TRUE,
];
}
}
return $extra;
}
/**
* Implements hook_module_implements_alter().
*/
function bynder_module_implements_alter(&$implementations, $hook) {
if ($hook == 'page_top') {
// Move bynder_page_top() to the end of the list to make sure it always runs
// after toolbar_page_top().
$group = $implementations['bynder'];
unset($implementations['bynder']);
$implementations['bynder'] = $group;
}
}
/**
* Implements hook_page_top().
*/
function bynder_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\\bynder\\Plugin\\EntityBrowser\\Widget\\BynderUpload') {
$page_top['toolbar']['#access'] = FALSE;
}
}
/**
* Returns most appropriate label for option.
*
* @param array $option
* The property array from Bynder.
*
* @return string
* Returns translated label for the current language if available or the
* default label.
*/
function bynder_get_applicable_label_translation(array $option) {
$current_language_id = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
$available_label_translations = [];
if (isset($option['labels'])) {
foreach ($option['labels'] as $langcode => $label) {
// Drupal doesn't differentiate by default en_US and en_GB. Here if we
// first get en_US and then en_GB we return the value from en_GB for the
// English language. If we get the reverse order the reverse is true.
$available_label_translations[substr($langcode, 0, 2)] = $label;
}
}
if (array_key_exists($current_language_id, $available_label_translations)) {
return $available_label_translations[$current_language_id];
}
return $option['label'];
}
/**
* Gets media restriction usage info.
*
* @param array|false $property_options
* Property options as returned from Bynder API.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* A label for a given restriction level.
*/
function get_media_restriction($property_options) {
if ($property_options && \Drupal::config('bynder.settings')
->get('usage_metaproperty')) {
$config_restrictions = array_combine([
t('Royality free'),
t('Web licence'),
t('Print licence'),
], [
\Drupal::config('bynder.settings')
->get('restrictions.royalty_free'),
\Drupal::config('bynder.settings')
->get('restrictions.web_license'),
\Drupal::config('bynder.settings')
->get('restrictions.print_license'),
]);
$restriction = array_keys(array_intersect($config_restrictions, $property_options));
return !empty($restriction) ? reset($restriction) : t('N/A');
}
else {
return t('N/A');
}
}
/**
* Implements hook_cron().
*/
function bynder_cron() {
$last_update = \Drupal::state()
->get('bynder_cache_last_update', 0);
if ($last_update && $last_update + \Drupal::config('default')
->get('cache_lifetime') < \Drupal::time()
->getRequestTime()) {
try {
\Drupal::service('bynder_api')
->updateCachedData();
} catch (\Exception $exception) {
\Drupal::logger('bynder')
->error(t('Unable to update Bynder caches. Check your <a href=":url">connection configuration</a>.', [
':url' => Url::fromRoute('bynder.configuration_form')
->toString(),
]));
}
}
// Update the metadata information of the local Bynder media entities.
\Drupal::service('bynder')
->updateLocalMetadataCron();
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function bynder_media_type_insert(MediaTypeInterface $media_type) {
// Do not alter configuration during config synch.
if ($media_type
->isSyncing()) {
return;
}
// Create a metadata field on new Bynder media types.
if ($media_type
->getSource() instanceof Bynder) {
/** @var \Drupal\bynder\Plugin\media\Source\Bynder $source */
$source = $media_type
->getSource();
$metadata_field_storage = FieldStorageConfig::loadByName('media', BynderMetadataItem::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();
}
}
Functions
Name | Description |
---|---|
bynder_cron | Implements hook_cron(). |
bynder_entity_extra_field_info | Implements hook_entity_extra_field_info(). |
bynder_form_alter | Implements hook_form_alter(). |
bynder_get_applicable_label_translation | Returns most appropriate label for option. |
bynder_help | Implements hook_help(). |
bynder_media_type_insert | Implements hook_ENTITY_TYPE_insert(). |
bynder_module_implements_alter | Implements hook_module_implements_alter(). |
bynder_page_top | Implements hook_page_top(). |
bynder_theme | Implements hook_theme(). |
get_media_restriction | Gets media restriction usage info. |
get_uri_to_bynder | Returns edit link for Bynder asset. |