You are here

taxonomy_image.module in Taxonomy Image 8

Same filename and directory in other branches
  1. 5 taxonomy_image.module
  2. 6 taxonomy_image.module
  3. 7 taxonomy_image.module

Implements field formatter that displays image on referenced taxonomy terms.

File

taxonomy_image.module
View source
<?php

/**
 * @file
 * Implements field formatter that displays image on referenced taxonomy terms.
 */
use Drupal\Core\Field\FieldException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Form submission handler for field_ui_display_overview_form().
 *
 * Attaches the taxonomy_image field to vocabulary terms, if the image formatter
 * is selected and the taxonomy_image field hasn't been instaciated for the
 * vocabulary yet.
 */
function taxonomy_image_display_overview_form_submit(array $form, FormStateInterface $form_state) {

  // Content type machine name.
  $type = $form['#bundle'];

  // Iterate over the fields that are using the
  // taxonomy_image_term_reference_image formatter.
  foreach ($form['#fields'] as $field_name) {
    $entity_type = 'node';
    $field_info = FieldStorageConfig::loadByName($entity_type, $field_name);
    if ($field_info) {
      if ($field_info
        ->getType() == 'entity_reference') {
        if ($form_state
          ->getValue('fields')[$field_name]['type'] == 'taxonomy_image_term_reference_image') {

          // If a field is available, don't do anything.
          // Lookup the associated vocabulary.
          $field_data = \Drupal::config('field.field.node.' . $type . "." . $field_name)
            ->get();
          $bundles = $field_data['settings']['handler_settings']['target_bundles'];
          $bundle = reset($bundles);

          // Ensure the taxonomy_image field exists.
          $entity_type_id = 'taxonomy_term';
          $image_field_name = 'taxonomy_image';
          if (!FieldConfig::loadByName($entity_type_id, $bundle, $image_field_name)) {
            if (!FieldStorageConfig::loadByName($entity_type_id, $image_field_name)) {
              try {
                FieldStorageConfig::create(array(
                  'field_name' => $image_field_name,
                  'entity_type' => 'taxonomy_term',
                  'type' => 'image',
                ))
                  ->save();
              } catch (FieldException $e) {
                drupal_set_message(t('The default image field has been deleted.'), 'warning');
              }
            }
            try {
              FieldConfig::create(array(
                'field_name' => $image_field_name,
                'entity_type' => 'taxonomy_term',
                'bundle' => $bundle,
                'label' => t('Image'),
                'description' => t('The image of this term.'),
                'required' => FALSE,
              ))
                ->save();

              // Tell the user where the field settings can be changed.
              $field_settings_url = '/admin/structure/taxonomy/manage/' . $bundle . '/overview/fields/taxonomy_term.' . $bundle . '.' . $image_field_name;
              $link = Link::fromTextAndUrl('field settings', Url::fromUri('internal:' . $field_settings_url))
                ->toString();
              drupal_set_message(t('An image field has been attached to the vocabulary terms. You can adjust the @field_settings.', array(
                '@field_settings' => $link,
              )));
            } catch (FieldException $e) {
              drupal_set_message(t('The default image field has been deleted.'), 'warning');
              return;
            }
          }
        }
      }
    }
  }
}

/**
 * Implements hook_form_FORM_BASE_ID_alter().
 */
function taxonomy_image_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state) {
  foreach (array_keys($form['actions']) as $action) {
    if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
      $form['actions'][$action]['#submit'][] = 'taxonomy_image_display_overview_form_submit';
    }
  }
}

Functions

Namesort descending Description
taxonomy_image_display_overview_form_submit Form submission handler for field_ui_display_overview_form().
taxonomy_image_form_entity_view_display_edit_form_alter Implements hook_form_FORM_BASE_ID_alter().