postoffice-1.0.x-dev/extensions/postoffice_file/postoffice_file.module

extensions/postoffice_file/postoffice_file.module
<?php

/**
 * @file
 * Primary module hooks for Postoffice File module.
 */

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;

/**
 * Implements hook_field_formatter_third_party_settings_form().
 */
function postoffice_file_field_formatter_third_party_settings_form(
  FormatterInterface $plugin,
  FieldDefinitionInterface $field_definition,
  $view_mode,
  array $form,
  FormStateInterface $form_state,
) {
  $element = [];

  if ($plugin->getPluginId() === 'file_default') {
    $element['adjustment'] = [
      '#type' => 'select',
      '#title' => t('HTML email adjustment'),
      '#options' => [
        'attach_remove' => t('Attach files, remove field'),
        'attach_transform_urls' => t('Attach files, use absolute URLs'),
        'transform_urls' => t('Absolute URLs'),
        'attach_display' => t('Attach files'),
      ],
      '#empty_option' => t('No adjustment'),
      '#default_value' => $plugin->getThirdPartySetting(
        'postoffice_file',
        'adjustment'
      ),
    ];
  }

  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary_alter().
 */
function postoffice_file_field_formatter_settings_summary_alter(&$summary, $context) {
  if ($context['formatter']->getPluginId() === 'file_default') {
    $adjustment = $context['formatter']->getThirdPartySetting(
      'postoffice_file',
      'adjustment'
    );

    switch ($adjustment) {
      case 'attach_remove':
        $summary[] = t('Attach files to HTML email and remove field');
        break;

      case 'attach_transform_urls':
        $summary[] = t('Attach files to HTML email and transform URLs');
        break;

      case 'transform_urls':
        $summary[] = t('Absolute URLs in HTML email');
        break;

      case 'attach_display':
        $summary[] = t('Attach files to HTML email');
        break;
    }
  }
}

/**
 * Implements hook_entity_display_build_alter().
 */
function postoffice_file_entity_display_build_alter(&$build, $context) {
  foreach (Element::children($build) as $field_name) {
    $element = &$build[$field_name];
    if (
      !empty($element['#field_type']) && $element['#field_type'] === 'file' &&
      !empty($element['#formatter']) && $element['#formatter'] === 'file_default' &&
      !empty($element['#third_party_settings']['postoffice_file']['adjustment'])
    ) {
      $adjustment = $element['#third_party_settings']['postoffice_file']['adjustment'];
      foreach (Element::children($element) as $delta) {
        $element[$delta]['#postoffice_file_adjustment'] = $adjustment;
      }
      if ($adjustment === 'attach_remove') {
        $element['#attributes']['data-postoffice-file-remove'] = TRUE;
      }
    }
  }
}

/**
 * Implements hook_theme_registry_alter().
 */
function postoffice_file_theme_registry_alter(&$theme_registry) {
  $theme_registry['file_link']['variables'] += [
    'postoffice_file_adjustment' => '',
  ];
}

/**
 * Implements hook_preprocess_HOOK() for file_link.
 *
 * @see template_preprocess_file_link()
 */
function postoffice_file_preprocess_file_link(&$variables) {
  switch ($variables['postoffice_file_adjustment']) {
    case 'attach_remove':
    case 'attach_display':
      $variables['attributes']['data-postoffice-file-attach-target-id'] = $variables['file']->id();
      break;

    case 'attach_transform_urls':
      $variables['attributes']['data-postoffice-file-attach-target-id'] = $variables['file']->id();
      $variables['attributes']['data-postoffice-file-transform-urls'] = TRUE;
      break;

    case 'transform_urls':
      $variables['attributes']['data-postoffice-file-transform-urls'] = TRUE;
      break;
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc