inline_media_form-1.0.0-beta1/inline_media_form.module
inline_media_form.module
<?php
/**
* @file
* Main code file for the Inline Media Form module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\inline_media_form\Plugin\Field\FieldWidget\InlineMediaFormWidget;
use Drupal\media_library\MediaLibraryState;
use Drupal\views\ViewExecutable;
// =============================================================================
// Hook Implementations.
// =============================================================================
/**
* Implements hook_theme().
*/
function inline_media_form_theme() {
return [
'inline_media_form_info_icon' => [
'variables' => [
'message' => NULL,
'icon' => NULL,
],
],
'inline_media_form_actions' => [
'render element' => 'element',
'template' => 'inline-media-form-actions',
],
'inline_media_form_summary' => [
'render element' => 'element',
'template' => 'inline-media-form-summary',
],
];
}
/**
* Implements hook_form_alter() for media library widget forms.
*
* This is used to customize the button text inside the media library modal when
* the modal is triggered by an Inline Media Form field, so it flows better with
* the text on the opener button.
*
* @noinspection PhpDocSignatureInspection
* @noinspection PhpUnusedParameterInspection
*/
function inline_media_form_form_alter(array &$form,
FormStateInterface $form_state,
string $form_id) {
$request = \Drupal::request();
if (isset($form['media_library_select_form']) &&
(strpos($form_id, 'views_form') === 0) &&
$request->query->has('media_library_opener_id')) {
$library_state = MediaLibraryState::fromRequest($request);
$opener_params = $library_state->getOpenerParameters();
$enclosing_widget_type = $opener_params['field_widget_type'] ?? NULL;
if ($enclosing_widget_type === InlineMediaFormWidget::class) {
$form["actions"]["submit"]["#value"] = t('Add selected');
}
}
}
/**
* Implements hook_preprocess_HOOK() for field_multiple_value_form().
*
* @noinspection PhpDocSignatureInspection
*/
function inline_media_form_preprocess_field_multiple_value_form(
array &$variables) {
$table =& $variables['table'];
$element =& $variables['element'];
$rows =& $table['#rows'];
if (!empty($table['#header']) && !empty($rows)) {
// Find inline_media_form_actions and move to header.
// @see template_preprocess_field_multiple_value_form()
$header = $rows[0]['data'][1] ?? [];
if (!empty($header['data']['#inline_media_form_header'])) {
$table['#header'][0]['data'] = [
'title' => $table['#header'][0]['data'],
'button' => $header['data'],
];
unset($rows[0]);
}
// Add the media file type as a class to every row.
if (isset($element['#inline_media_form_widget'])) {
foreach ($rows as $key => $row) {
$first_row_data = $row['data'][1]['data'];
$media_file_type = $first_row_data['#media_file_type'] ?? NULL;
if ($media_file_type !== NULL) {
$rows[$key]['class'][] =
'media-file-type--' . str_replace('_', '-', $media_file_type);
}
}
}
}
// Remove the drag handler if we are translating, if the field's cardinality
// is 1 or if there are no media file added. Passing through this will not
// only remove the drag handler but also the order column that is empty when
// no media files are added and when the field is single value.
if ((isset($element['#allow_reference_changes']) &&
!$element['#allow_reference_changes']) ||
(isset($element['#cardinality']) && ($element['#cardinality'] == 1)) ||
empty($rows)) {
if (isset($table['#tabledrag'])) {
// Remove the tabledrag.
unset($table['#tabledrag']);
unset($table['#header'][1]);
foreach ($rows as $key => $value) {
$rows[$key]['data'][0]['class'][] = 'media-file-bullet';
// Restore the removed weight and give access FALSE.
$rows[$key]['data'][1]['data']['_weight'] = $value['data'][2]['data'];
unset($rows[$key]['data'][2]);
$rows[$key]['data'][1]['data']['_weight']['#access'] = FALSE;
}
}
}
}
// =============================================================================
// Theme Preprocess Functions.
// =============================================================================
/**
* Prepares variables for inline_media_form_actions component.
*
* Default template: inline-media-form-actions.html.twig.
*
* @param array $variables
* An associative array containing the render element under a key called
* 'element'. Additional variables from the element are added to this array
* before each element is passed-in to the template as a variable in scope.
*/
function template_preprocess_inline_media_form_actions(array &$variables) {
// Define variables for the template.
$variables += [
'actions' => [],
'dropdown_actions' => [],
'toolbar' => [],
];
$element = $variables['element'];
foreach (array_keys($variables) as $variable_name) {
if (!empty($element[$variable_name])) {
$variables[$variable_name] = $element[$variable_name];
}
}
}
/**
* Prepares variables for inline_media_form_summary components.
*
* Default template: inline-media-form-summary.html.twig.
*
* @param array $variables
* An associative array containing:
* - content: The content to summarize.
* - expanded: Whether or not the element is expanded or collapsed.
*/
function template_preprocess_inline_media_form_summary(array &$variables) {
$element =& $variables['element'];
$variables['content'] = $element['#summary']['content'];
$variables['expanded'] = !empty($element['#expanded']);
}
/**
* Implements hook_views_pre_render().
*
* Attach the JavaScript file which implements the "Select all" behavior.
*/
function inline_media_form_views_pre_render(ViewExecutable $view) {
if ($view->storage->id() === 'media_library_imported') {
$view->element['#attached']['library'][] = 'inline_media_form/select-all';
}
}
