You are here

class FileMiconFormatter in Micon 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Field/FieldFormatter/FileMiconFormatter.php \Drupal\micon\Plugin\Field\FieldFormatter\FileMiconFormatter

Plugin implementation of the 'file_micon' formatter.

Plugin annotation


@FieldFormatter(
  id = "file_micon",
  label = @Translation("Micon"),
  field_types = {
    "file"
  }
)

Hierarchy

Expanded class hierarchy of FileMiconFormatter

File

src/Plugin/Field/FieldFormatter/FileMiconFormatter.php, line 22

Namespace

Drupal\micon\Plugin\Field\FieldFormatter
View source
class FileMiconFormatter extends FileFormatterBase {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    $settings = [
      'title' => 'View',
      'icon' => 'fa-file',
      'position' => 'before',
      'target' => '',
      'text_only' => '',
    ];
    foreach (self::mimeGroups() as $id => $data) {
      $key = 'icon_' . $id;
      $settings[$key] = $data['icon'];
    }
    return $settings + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $summary[] = t('Link title as @title', [
      '@title' => $this
        ->getSetting('title') ? $this
        ->getSetting('title') : 'Default',
    ]);
    if ($position = $this
      ->getSetting('position')) {
      $summary[] = t('Icon position: @value', [
        '@value' => ucfirst($position),
      ]);
    }
    if ($this
      ->getSetting('text_only')) {
      $summary[] = t('Text only');
    }
    else {
      if ($this
        ->getSetting('target')) {
        $summary[] = t('Open link in new window');
      }
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements = parent::settingsForm($form, $form_state);
    $elements['title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Link title'),
      '#default_value' => $this
        ->getSetting('title'),
    ];
    $elements['text_only'] = [
      '#type' => 'checkbox',
      '#title' => t('Text only'),
      '#default_value' => $this
        ->getSetting('text_only'),
    ];
    foreach (self::mimeGroups() as $id => $data) {
      $key = 'icon_' . $id;
      $elements['icon_' . $key] = [
        '#type' => 'micon',
        '#title' => $this
          ->t('Icon for %type', [
          '%type' => $data['label'],
        ]),
        '#default_value' => $this
          ->getSetting($key),
      ];
    }
    $elements['position'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Icon position'),
      '#options' => [
        'before' => $this
          ->t('Before'),
        'after' => $this
          ->t('After'),
      ],
      '#default_value' => $this
        ->getSetting('position'),
      '#required' => TRUE,
    ];
    $elements['target'] = [
      '#type' => 'checkbox',
      '#title' => t('Open link in new window'),
      '#return_value' => '_blank',
      '#default_value' => $this
        ->getSetting('target'),
      '#states' => [
        'invisible' => [
          ':input[name*="text_only"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($this
      ->getEntitiesToView($items, $langcode) as $delta => $file) {
      $item = $file->_referringItem;
      $url = file_create_url($file
        ->getFileUri());
      $options = [];
      if ($this
        ->getSetting('target')) {
        $options['attributes']['target'] = '_blank';
      }
      $link_text = $this
        ->getSetting('title') ? $this
        ->getSetting('title') : $item->description;
      $position = $this
        ->getSetting('position');
      $icon = $this
        ->mimeMap($file
        ->getMimeType());
      $link_text = micon($link_text)
        ->setIcon($this
        ->mimeMap($file
        ->getMimeType()));
      if ($position == 'after') {
        $link_text
          ->setIconAfter();
      }
      if ($this
        ->getSetting('text_only')) {
        $elements[$delta]['#markup'] = $link_text;
      }
      else {
        $elements[$delta] = Link::fromTextAndUrl($link_text, Url::fromUri($url, $options))
          ->toRenderable();
      }
      $elements[$delta]['#cache']['tags'] = $file
        ->getCacheTags();

      // Pass field item attributes to the theme function.
      if (isset($item->_attributes)) {
        $elements[$delta] += [
          '#attributes' => [],
        ];
        $elements[$delta]['#attributes'] += $item->_attributes;

        // Unset field item attributes since they have been included in the
        // formatter output and should not be rendered in the field template.
        unset($item->_attributes);
      }
    }
    return $elements;
  }

  /**
   * Get mime groups.
   */
  public static function mimeGroups() {
    return [
      'default' => [
        'label' => t('Default'),
        'icon' => 'fa-file',
      ],
      'image' => [
        'label' => t('Image'),
        'icon' => 'fa-file-image',
      ],
      'document' => [
        'label' => t('Document'),
        'icon' => 'fa-file-word',
      ],
      'spreadsheet' => [
        'label' => t('Spreadsheet'),
        'icon' => 'fa-file-excel',
      ],
      'presentation' => [
        'label' => t('Presentation'),
        'icon' => 'fa-file-powerpoint',
      ],
      'archive' => [
        'label' => t('Archive'),
        'icon' => 'fa-file-archive',
      ],
      'script' => [
        'label' => t('Script'),
        'icon' => 'fa-file-code',
      ],
      'html' => [
        'label' => t('HTML'),
        'icon' => 'fa-file-code',
      ],
      'executable' => [
        'label' => t('Executable'),
        'icon' => 'fa-file-exclamation',
      ],
      'pdf' => [
        'label' => t('PDF'),
        'icon' => 'fa-file-pdf',
      ],
    ];
  }

  /**
   * Mime icon options.
   */
  protected function mimeMap($mime_type) {
    switch ($mime_type) {

      // Image types.
      case 'image/jpeg':
      case 'image/png':
      case 'image/gif':
      case 'image/bmp':
        return $this
          ->getSetting('icon_image');

      // Word document types.
      case 'application/msword':
      case 'application/vnd.ms-word.document.macroEnabled.12':
      case 'application/vnd.oasis.opendocument.text':
      case 'application/vnd.oasis.opendocument.text-template':
      case 'application/vnd.oasis.opendocument.text-master':
      case 'application/vnd.oasis.opendocument.text-web':
      case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
      case 'application/vnd.stardivision.writer':
      case 'application/vnd.sun.xml.writer':
      case 'application/vnd.sun.xml.writer.template':
      case 'application/vnd.sun.xml.writer.global':
      case 'application/vnd.wordperfect':
      case 'application/x-abiword':
      case 'application/x-applix-word':
      case 'application/x-kword':
      case 'application/x-kword-crypt':
        return $this
          ->getSetting('icon_document');

      // Spreadsheet document types.
      case 'application/vnd.ms-excel':
      case 'application/vnd.ms-excel.sheet.macroEnabled.12':
      case 'application/vnd.oasis.opendocument.spreadsheet':
      case 'application/vnd.oasis.opendocument.spreadsheet-template':
      case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
      case 'application/vnd.stardivision.calc':
      case 'application/vnd.sun.xml.calc':
      case 'application/vnd.sun.xml.calc.template':
      case 'application/vnd.lotus-1-2-3':
      case 'application/x-applix-spreadsheet':
      case 'application/x-gnumeric':
      case 'application/x-kspread':
      case 'application/x-kspread-crypt':
        return $this
          ->getSetting('icon_spreadsheet');

      // Presentation document types.
      case 'application/vnd.ms-powerpoint':
      case 'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
      case 'application/vnd.oasis.opendocument.presentation':
      case 'application/vnd.oasis.opendocument.presentation-template':
      case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
      case 'application/vnd.stardivision.impress':
      case 'application/vnd.sun.xml.impress':
      case 'application/vnd.sun.xml.impress.template':
      case 'application/x-kpresenter':
        return $this
          ->getSetting('icon_presentation');

      // Compressed archive types.
      case 'application/zip':
      case 'application/x-zip':
      case 'application/stuffit':
      case 'application/x-stuffit':
      case 'application/x-7z-compressed':
      case 'application/x-ace':
      case 'application/x-arj':
      case 'application/x-bzip':
      case 'application/x-bzip-compressed-tar':
      case 'application/x-compress':
      case 'application/x-compressed-tar':
      case 'application/x-cpio-compressed':
      case 'application/x-deb':
      case 'application/x-gzip':
      case 'application/x-java-archive':
      case 'application/x-lha':
      case 'application/x-lhz':
      case 'application/x-lzop':
      case 'application/x-rar':
      case 'application/x-rpm':
      case 'application/x-tzo':
      case 'application/x-tar':
      case 'application/x-tarz':
      case 'application/x-tgz':
        return $this
          ->getSetting('icon_archive');

      // Script file types.
      case 'application/ecmascript':
      case 'application/javascript':
      case 'application/mathematica':
      case 'application/vnd.mozilla.xul+xml':
      case 'application/x-asp':
      case 'application/x-awk':
      case 'application/x-cgi':
      case 'application/x-csh':
      case 'application/x-m4':
      case 'application/x-perl':
      case 'application/x-php':
      case 'application/x-ruby':
      case 'application/x-shellscript':
      case 'text/vnd.wap.wmlscript':
      case 'text/x-emacs-lisp':
      case 'text/x-haskell':
      case 'text/x-literate-haskell':
      case 'text/x-lua':
      case 'text/x-makefile':
      case 'text/x-matlab':
      case 'text/x-python':
      case 'text/x-sql':
      case 'text/x-tcl':
        return $this
          ->getSetting('icon_script');

      // HTML aliases.
      case 'application/xhtml+xml':
        return $this
          ->getSetting('icon_html');

      // Executable types.
      case 'application/x-macbinary':
      case 'application/x-ms-dos-executable':
      case 'application/x-pef-executable':
        return $this
          ->getSetting('icon_executable');

      // Acrobat types.
      case 'application/pdf':
      case 'application/x-pdf':
      case 'applications/vnd.pdf':
      case 'text/pdf':
      case 'text/x-pdf':
        return $this
          ->getSetting('icon_pdf');
      default:
        return $this
          ->getSetting('icon_default');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityReferenceFormatterBase::getEntitiesToView protected function Returns the referenced entities for display. 1
EntityReferenceFormatterBase::prepareView public function Loads the entities referenced in that field across all the entities being viewed. Overrides FormatterBase::prepareView
EntityReferenceFormatterBase::view public function Overrides FormatterBase::view
FileFormatterBase::checkAccess protected function Checks access to the given entity. Overrides EntityReferenceFormatterBase::checkAccess
FileFormatterBase::needsEntityLoad protected function Returns whether the entity referenced by an item needs to be loaded. Overrides EntityReferenceFormatterBase::needsEntityLoad 1
FileMiconFormatter::defaultSettings public static function Defines the default settings for this plugin. Overrides PluginSettingsBase::defaultSettings
FileMiconFormatter::mimeGroups public static function Get mime groups.
FileMiconFormatter::mimeMap protected function Mime icon options.
FileMiconFormatter::settingsForm public function Returns a form to configure settings for the formatter. Overrides FormatterBase::settingsForm
FileMiconFormatter::settingsSummary public function Returns a short summary for the current formatter settings. Overrides FormatterBase::settingsSummary
FileMiconFormatter::viewElements public function Builds a renderable array for a field value. Overrides FormatterInterface::viewElements
FormatterBase::$fieldDefinition protected property The field definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 11
FormatterBase::getFieldSetting protected function Returns the value of a field setting.
FormatterBase::getFieldSettings protected function Returns the array of field settings.
FormatterBase::isApplicable public static function Returns if the formatter can be used for the provided field. Overrides FormatterInterface::isApplicable 14
FormatterBase::__construct public function Constructs a FormatterBase object. Overrides PluginBase::__construct 11
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::$thirdPartySettings protected property The plugin settings injected by third party modules.
PluginSettingsBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 6
PluginSettingsBase::getSetting public function Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
PluginSettingsBase::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
PluginSettingsBase::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface::onDependencyRemoval 3
PluginSettingsBase::setSetting public function Sets the value of a setting for the plugin. Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Sets the settings for the plugin. Overrides PluginSettingsInterface::setSettings
PluginSettingsBase::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
PluginSettingsBase::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.