filefield_sources_jsonapi-8.x-1.0-beta9/filefield_sources_jsonapi.module
filefield_sources_jsonapi.module
<?php
/**
* @file
* Module file for the Tieto Media Library entioty browser.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_help().
*/
function filefield_sources_jsonapi_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.filefield_sources_jsonapi':
return check_markup(file_get_contents(dirname(__FILE__) . '/README.md'));
}
}
/**
* Implements hook_theme().
*/
function filefield_sources_jsonapi_theme($existing, $type, $theme, $path) {
return [
'browser_media_box' => [
'variables' => [
'checkbox' => [],
'checkbox_id' => '',
'img' => [],
'title' => '',
],
],
];
}
/**
* Implements hook_element_info_alter().
*/
function filefield_sources_jsonapi_element_info_alter(&$type) {
if (isset($type['managed_file'])) {
$type['managed_file']['#process'][] = 'filefield_sources_jsonapi_field_process';
}
}
/**
* A #process callback to extend the filefield_widget element type.
*
* Add widget.js if source_remote_jsonapi is enabled.
*/
function filefield_sources_jsonapi_field_process(&$element, FormStateInterface $form_state, &$complete_form) {
// Check if we are processing file field sources.
if (!isset($element['#filefield_sources_settings']['source_remote_jsonapi'])) {
return $element;
}
$element['#attached']['library'][] = 'filefield_sources_jsonapi/widget';
if (!empty($element['#value']['fids'])) {
if (isset($element['#value']['filefield_remote_jsonapi']['alt'])) {
$element['#value']['alt'] = $element['#value']['filefield_remote_jsonapi']['alt'];
}
if (isset($element['#value']['filefield_remote_jsonapi']['title'])) {
$element['#value']['title'] = $element['#value']['filefield_remote_jsonapi']['title'];
}
if (isset($element['#value']['filefield_remote_jsonapi']['description'])) {
$element['#value']['description'] = $element['#value']['filefield_remote_jsonapi']['description'];
}
}
return $element;
}
/**
* Implements hook_preprocess_HOOK().
*
* Hide JSON API source selector if base settings are not set.
* Remove sources TAB if only one element left.
*/
function filefield_sources_jsonapi_preprocess_filefield_sources_list(&$variables) {
if ($variables['sources']['remote_jsonapi'] && !isset($variables['source_remote_jsonapi']['sources'])) {
unset($variables['sources']['remote_jsonapi']);
}
if (count($variables['sources']) < 2) {
$variables['sources'] = [];
}
}
/**
* Implements hook_page_attachments().
*
* Add ie=edge meta to html head on node add, node edit pages.
*/
function filefield_sources_jsonapi_page_attachments(array &$page) {
$route_name = Drupal::request()->attributes->get('_route');
if (in_array($route_name, ['entity.node.edit_form', 'node.add'])) {
$xuacompatible = [
'#tag' => 'meta',
'#attributes' => [
'http-equiv' => 'x-ua-compatible',
'content' => 'ie=edge',
],
];
$page['#attached']['html_head'][] = [$xuacompatible, 'x-ua-compatible'];
}
}
