You are here

field_group_link.module in Field Group Link 7

field_group_link.module

Defines a field group display formatter that is a link.

File

field_group_link.module
View source
<?php

/**
 * @file
 * field_group_link.module
 *
 * Defines a field group display formatter that is a link.
 */

/**
 * Implements hook_field_group_formatter_info().
 *
 * Define the field group link display formatter.
 */
function field_group_link_field_group_formatter_info() {
  return array(
    'display' => array(
      'link' => array(
        'label' => t('Link'),
        'format_types' => array(
          'open',
        ),
        'default_formatter' => 'open',
        'instance_settings' => array(
          'link_to' => '_none',
          'classes' => '',
          'custom_url' => '',
          'custom_url_normalize' => FALSE,
          'target' => 'default',
        ),
      ),
    ),
  );
}

/**
 * Implements hook_field_group_formatter_settings().
 *
 * Provide a settings form for the field group link formatter.
 */
function field_group_link_field_group_format_settings($group) {
  if ($group->format_type == 'link') {
    $options = array();
    $entity_info = entity_get_info($group->entity_type);
    $options['_none'] = t('Please select');

    // Check if this entity type has a uri callback.
    // @See entity_uri()
    if (isset($entity_info['bundles'][$group->bundle]['uri callback']) || isset($entity_info['uri callback'])) {
      $options['entity'] = t('Full !entity_type page', array(
        '!entity_type' => $entity_info['label'],
      ));
    }
    $options['custom_url'] = t('Custom URL');
    $fields = field_info_instances($group->entity_type, $group->bundle);
    foreach ($fields as $field_name => $field) {
      $field_info = field_info_field($field_name);
      if ($field_info['type'] == 'link_field') {
        $options[$field_name] = t('!field_label (Link field)', array(
          '!field_label' => $field['label'],
        ));
      }
      elseif ($field_info['type'] == 'entityreference') {
        $options[$field_name] = t('!field_label (Entity reference field)', array(
          '!field_label' => $field['label'],
        ));
      }
      elseif ($field_info['type'] == 'file') {
        $options[$field_name] = t('!field_label (File field)', array(
          '!field_label' => $field['label'],
        ));
      }
    }
    $value = isset($group->format_settings['instance_settings']['link_to']) ? $group->format_settings['instance_settings']['link_to'] : '_none';
    $form['instance_settings']['link_to'] = array(
      '#title' => t('Link to'),
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $value,
      '#element_validate' => array(
        'field_group_element_validate_not_none',
      ),
    );
    $form['instance_settings']['custom_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Custom URL'),
      '#description' => t('You may use tokens for this URL if you have token module.'),
      '#default_value' => isset($group->format_settings['instance_settings']['custom_url']) ? $group->format_settings['instance_settings']['custom_url'] : '',
      '#states' => array(
        'visible' => array(
          array(
            ':input[name="fields[' . $group->group_name . '][format_settings][settings][instance_settings][link_to]"]' => array(
              array(
                'value' => 'custom_url',
              ),
            ),
          ),
        ),
      ),
    );
    $form['instance_settings']['custom_url_normalize'] = array(
      '#type' => 'checkbox',
      '#title' => t('Normalize URL'),
      '#description' => t('Normalize URL by replacing spaces with dashes and transforming it to lowercase.'),
      '#default_value' => isset($group->format_settings['instance_settings']['custom_url_normalize']) ? $group->format_settings['instance_settings']['custom_url_normalize'] : TRUE,
      '#states' => array(
        'visible' => array(
          array(
            ':input[name="fields[' . $group->group_name . '][format_settings][settings][instance_settings][link_to]"]' => array(
              array(
                'value' => 'custom_url',
              ),
            ),
          ),
        ),
      ),
    );
    if (module_exists('token')) {
      $form['instance_settings']['tokens'] = array(
        '#title' => t('Tokens'),
        '#type' => 'container',
        '#states' => array(
          'visible' => array(
            ':input[name="fields[' . $group->group_name . '][format_settings][settings][instance_settings][link_to]"]' => array(
              array(
                'value' => 'custom_url',
              ),
            ),
          ),
        ),
      );
      $form['instance_settings']['tokens']['help'] = array(
        '#theme' => 'token_tree',
        '#token_types' => 'all',
        '#global_types' => FALSE,
        '#dialog' => TRUE,
      );
    }
    $target_description = t('Set the target for this link.');
    if (module_exists('link')) {
      $target_description .= '<br/>' . t("If you linking to a link field, the target specified on the link field's instance settings will override this value.");
    }
    $form['instance_settings']['target'] = array(
      '#title' => t('Target'),
      '#type' => 'select',
      '#description' => $target_description,
      '#options' => array(
        'default' => t('Default'),
        '_blank' => t('Blank (new tab)'),
      ),
      '#default_value' => isset($group->format_settings['instance_settings']['target']) ? $group->format_settings['instance_settings']['target'] : 'default',
    );
    return $form;
  }
}

/**
 * Form element validation handler for elements that must be not be _none.
 */
function field_group_element_validate_not_none($element, &$form_state) {
  $value = $element['#value'];
  if ($value == '_none') {
    form_error($element, t('Please select a value for %name.', array(
      '%name' => $element['#title'],
    )));
  }
}

/**
 * Implements hook_field_group_pre_render().
 *
 * Render the field group link.
 */
function field_group_link_field_group_pre_render(&$element, $group, &$form) {
  if ($group->format_type == 'link') {
    if ($group->format_settings['instance_settings']['link_to'] == '_none') {
      return;
    }

    // Get the entity key from the entity type.
    $entity_key = '#' . $form['#entity_type'];
    if (!isset($form[$entity_key])) {

      // Some entity types store the key in an arbitrary name.
      // Check for the ones that we know of.
      switch ($form['#entity_type']) {
        case 'taxonomy_term':
          $entity_key = '#term';
          break;
        case 'user':
          $entity_key = '#account';
          break;

        // Otherwise just search for #entity.
        // The Bean module is one known to use this.
        default:
          $entity_key = '#entity';
      }
    }
    if (isset($form[$entity_key]) && is_object($form[$entity_key])) {
      $entity = $form[$entity_key];
    }
    else {

      // We can't find the entity.
      // There's nothing we can do, so avoid attempting to create a link.
      return;
    }
    $attributes = array(
      'class' => drupal_html_class($group->group_name),
    );
    if (isset($group->format_settings['instance_settings']['target']) && $group->format_settings['instance_settings']['target'] != 'default') {
      $attributes['target'] = $group->format_settings['instance_settings']['target'];
    }
    if (!empty($group->format_settings['instance_settings']['classes'])) {
      $attributes['class'] = $group->format_settings['instance_settings']['classes'];
    }
    $url = NULL;
    $options = array();
    if ($group->format_settings['instance_settings']['link_to'] == 'entity') {
      $entity_url = entity_uri($form['#entity_type'], $entity);
      $url = $entity_url['path'];
    }
    elseif ($group->format_settings['instance_settings']['link_to'] == 'custom_url') {
      $url = $group->format_settings['instance_settings']['custom_url'];
      if (module_exists('token')) {
        $url = token_replace($url, array(
          $form['#entity_type'] => $entity,
        ), array(
          'clear' => TRUE,
          'sanitize' => TRUE,
        ));
      }
      if ($group->format_settings['instance_settings']['custom_url_normalize']) {
        $url = drupal_strtolower(str_replace(" ", "-", $url));
      }
    }
    else {
      $field_name = $group->format_settings['instance_settings']['link_to'];
      if (isset($entity->{$field_name})) {
        $field = field_get_items($group->entity_type, $entity, $group->format_settings['instance_settings']['link_to']);
        $field_info = field_info_field($field_name);
        if (!empty($field[0])) {
          if ($field_info['type'] == 'link_field' && !empty($field[0]['url'])) {
            $url = $field[0]['url'];
            if (isset($field[0]['attributes'])) {
              $attributes = array_merge_recursive($attributes, $field[0]['attributes']);
            }

            // If the url has a query attached to it, add it to $options.
            if (isset($field[0]['query'])) {
              $options['query'] = $field[0]['query'];
            }

            // If a fragment is set, add it to $options.
            if (isset($field[0]['fragment'])) {
              $options['fragment'] = $field[0]['fragment'];
            }

            // Add title if set.
            if (!empty($field[0]['title']) && empty($attributes['title'])) {
              $attributes['title'] = $field[0]['title'];
            }
          }
          elseif ($field_info['type'] == 'entityreference' && !empty($field[0]['target_id'])) {
            $target_entity_type = $field_info['settings']['target_type'];
            $target_entity = current(entity_load($target_entity_type, array(
              $field[0]['target_id'],
            )));
            if ($target_entity) {
              $entity_url = entity_uri($target_entity_type, $target_entity);
              $url = $entity_url['path'];
            }
          }
          elseif ($field_info['type'] == 'file' && !empty($field[0]['uri'])) {
            if (!empty($field[0]['title'])) {
              $attributes['title'] = $field[0]['title'];
            }
            elseif (!empty($field[0]['description'])) {
              $attributes['title'] = $field[0]['description'];
            }
            else {
              $attributes['title'] = $field[0]['filename'];
            }
            $url = file_create_url($field[0]['uri']);
          }
        }
      }
    }
    if ($url) {
      $element['#prefix'] = '<a href="' . url($url, $options) . '"' . drupal_attributes($attributes) . '>';
      $element['#suffix'] = '</a>';
    }
  }
}

Functions

Namesort descending Description
field_group_element_validate_not_none Form element validation handler for elements that must be not be _none.
field_group_link_field_group_formatter_info Implements hook_field_group_formatter_info().
field_group_link_field_group_format_settings Implements hook_field_group_formatter_settings().
field_group_link_field_group_pre_render Implements hook_field_group_pre_render().