You are here

icon_field.module in Icon API 7

icon_field.module Provides a field for attaching an icon to a fieldable entity.

File

modules/icon_field/icon_field.module
View source
<?php

/**
 * @file
 * icon_field.module
 * Provides a field for attaching an icon to a fieldable entity.
 */

/**
 * Implements hook_field_info().
 */
function icon_field_field_info() {
  return array(
    'icon_field' => array(
      'label' => t('Icon'),
      'description' => t('Store a bundle and icon in the database to assemble an icon field.'),
      'settings' => array(
        'bundle' => NULL,
        'icon' => NULL,
        'wrapper' => NULL,
        'wrapper_class' => NULL,
      ),
      'instance_settings' => array(
        'bundle' => NULL,
        'icon' => NULL,
      ),
      'default_widget' => 'icon_field',
      'default_formatter' => 'icon_field_default',
    ),
  );
}

/**
 * Implements hook_field_is_empty().
 */
function icon_field_field_is_empty($item, $field) {
  return empty($item['bundle']) && empty($item['icon']);
}

/**
 * Implements hook_field_instance_settings_form().
 */
function icon_field_field_instance_settings_form($field, $instance) {
  $form = array();
  return $form;
}

/**
 * Implements hook_field_widget_info().
 */
function icon_field_field_widget_info() {
  return array(
    'icon_field' => array(
      'label' => 'Icon',
      'field types' => array(
        'icon_field',
      ),
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function icon_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $element += array(
    '#type' => 'icon_selector',
    '#default_bundle' => isset($items[$delta]['bundle']) ? $items[$delta]['bundle'] : '',
    '#default_icon' => isset($items[$delta]['icon']) ? $items[$delta]['icon'] : '',
    '#default_wrapper' => isset($items[$delta]['wrapper']) ? $items[$delta]['wrapper'] : '',
    '#default_wrapper_class' => isset($items[$delta]['wrapper_class']) ? $items[$delta]['wrapper_class'] : '',
  );
  return $element;
}

/**
 * Implements hook_field_formatter_info().
 */
function icon_field_field_formatter_info() {
  return array(
    'icon_field_default' => array(
      'label' => t('Icon'),
      'field types' => array(
        'icon_field',
      ),
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
      'settings' => array(
        'link_to_content' => FALSE,
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function icon_field_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $element = array();
  if ($display['type'] == 'icon_field_default') {
    $element['link_to_content'] = array(
      '#title' => t('Link icon to content'),
      '#type' => 'checkbox',
      '#default_value' => $settings['link_to_content'],
    );
  }
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function icon_field_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  if ($display['type'] == 'icon_field_default') {
    if ($display['settings']['link_to_content']) {
      return t('Link icon to content');
    }
    else {
      return t('No link');
    }
  }
  return '';
}

/**
 * Implements hook_field_formatter_view().
 */
function icon_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $elements = array();
  foreach ($items as $delta => $item) {
    $item += array(
      'wrapper' => '',
      'wrapper_class' => '',
    );
    if ($display['settings']['link_to_content']) {
      $uri = entity_uri($entity_type, $entity);
      $options = array(
        'html' => TRUE,
        'attributes' => array(),
      );
      if (isset($uri['options']) and !empty($uri['options'])) {
        $options = array_merge($uri['options'], $options);
      }
      $elements[$delta] = array(
        '#theme' => 'link',
        '#path' => $uri['path'],
        '#text' => theme('icon', $item),
        '#options' => $options,
      );
    }
    else {
      $elements[$delta] = array(
        '#theme' => 'icon',
        '#bundle' => $item['bundle'],
        '#icon' => $item['icon'],
        '#wrapper' => $item['wrapper'],
        '#wrapper_class' => $item['wrapper_class'],
      );
    }
  }
  return $elements;
}