You are here

farm_fields.module in farmOS 7

File

modules/farm/farm_fields/farm_fields.module
View source
<?php

/**
 * @file
 * Farm fields module.
 */
include_once 'farm_fields.features.inc';

/**
 * Implements hook_farm_ui_entities().
 */
function farm_fields_farm_ui_entities() {
  return array(
    'taxonomy_term' => array(
      'farm_log_categories' => array(
        'label' => t('Log category'),
        'label_plural' => t('Log categories'),
      ),
    ),
  );
}

/**
 * Implements hook_farm_access_perms().
 */
function farm_fields_farm_access_perms($role) {
  $perms = array();

  // Grant access to the farm input filter.
  $perms[] = 'use text format farm_format';
  return $perms;
}

/**
 * Implements hook_field_default_field_instances_alter().
 */
function farm_fields_field_default_field_instances_alter(&$fields) {

  // Set the acceptable file extensions on all file and image field instances.
  foreach ($fields as &$field) {
    if ($field['field_name'] == 'field_farm_images') {
      $field['settings']['file_extensions'] = farm_fields_file_types('image');
    }
    elseif ($field['field_name'] == 'field_farm_files') {
      $field['settings']['file_extensions'] = farm_fields_file_types('file');
    }
  }
}

/**
 * Returns a list of acceptable file types for image/file fields.
 *
 * @param string $type
 *   Can be 'image', 'file', or empty (to return all).
 * @param string $separator
 *   Separator string to put between type in the output.
 *
 * @return string
 *   Returns a string of types.
 */
function farm_fields_file_types($type = '', $separator = ' ') {
  $file_types = array(
    'image' => array(
      'png',
      'gif',
      'jpg',
      'jpeg',
    ),
    'file' => array(
      'csv',
      'doc',
      'docx',
      'logz',
      'odt',
      'odp',
      'ods',
      'pdf',
      'ppt',
      'pptx',
      'txt',
      'xls',
      'xlsx',
      'kml',
      'kmz',
      'mp3',
      'wav',
      'ogg',
    ),
  );
  if ($type == 'image') {
    $types = $file_types['image'];
  }
  elseif ($type == 'file') {
    $types = $file_types['file'];
  }
  else {
    $types = array_merge($file_types['image'], $file_types['file']);
  }
  return implode($separator, $types);
}

/**
 * Helper function for pre-populating entityreference fields in entity forms.
 *
 * @param array $form
 *   The entity form array to modify, passed by reference.
 * @param string $entity_type
 *   The entity type that is being referenced.
 * @param string $field_name
 *   The machine name of the entity reference field.
 * @param array $entity_ids
 *   An array of entities to add to the references.
 */
function farm_fields_prepopulate_entityreference(&$form, $entity_type, $field_name, $entity_ids) {

  // Load the field instance definition.
  $form_entity_type = $form['#entity_type'];
  $form_entity_bundle = $form['#bundle'];
  $field_base = field_info_field($field_name);
  $field_instance = field_info_instance($form_entity_type, $field_name, $form_entity_bundle);

  // Set the field value key based on the field type.
  $value_key = 'value';
  switch ($field_base['type']) {
    case 'entityreference':
      $value_key = 'target_id';
      break;
    case 'taxonomy_term_reference':
      $value_key = 'tid';
      break;
  }

  // Validate the entity IDs by loading them. Rebuild the list of IDs using
  // only the entities that loaded.
  $entities = entity_load($entity_type, $entity_ids);
  $entity_ids = array();
  foreach ($entities as $entity) {
    $entity_ids[] = entity_id($entity_type, $entity);
  }

  // If there are no entity IDs, bail.
  if (empty($entity_ids)) {
    return;
  }

  // If the widget type is "radios/checkboxes" or "select list"...
  if (in_array($field_instance['widget']['type'], array(
    'options_buttons',
    'options_select',
  ))) {

    // Use the array of IDs as the field's default value.
    if (empty($form[$field_name][LANGUAGE_NONE]['#default_value'])) {
      $form[$field_name][LANGUAGE_NONE]['#default_value'] = $entity_ids;
    }
  }
  elseif (in_array($field_instance['widget']['type'], array(
    'entityreference_autocomplete',
    'entityreference_autocomplete_tags',
  ))) {

    // Build a list of entity labels in the format that the widget expects.
    $labels = array();
    foreach ($entities as $id => $entity) {
      $labels[] = entity_label($entity_type, $entity) . ' (' . $id . ')';
    }

    // For "autocomplete", add each one as a separate field.
    if ($field_instance['widget']['type'] == 'entityreference_autocomplete') {
      foreach ($labels as $key => $label) {

        // If the item isn't empty, skip it.
        if (!empty($form[$field_name][LANGUAGE_NONE][$key][$value_key]['#default_value'])) {
          continue;
        }

        /**
         * @todo
         * This seems to be the easiest way to auto-populate entityreference_autocomplete
         * widgets, but it is MESSY! If anyone can figure out a better way, I will buy
         * you a beer.
         */

        // Copy the initial array structure from the first element.
        $form[$field_name][LANGUAGE_NONE][$key] = $form[$field_name][LANGUAGE_NONE][0];

        // Set the default, delta, and weight values.
        $form[$field_name][LANGUAGE_NONE][$key][$value_key]['#default_value'] = $label;
        $form[$field_name][LANGUAGE_NONE][$key][$value_key]['#delta'] = $key;
        $form[$field_name][LANGUAGE_NONE][$key][$value_key]['#weight'] = $key;

        // Only make the first one required.
        if ($key > 0) {
          $form[$field_name][LANGUAGE_NONE][$key][$value_key]['#required'] = 0;
        }
        $form[$field_name][LANGUAGE_NONE]['#max_delta'] = $key;
        $form[$field_name][LANGUAGE_NONE][$key]['_weight']['#delta'] = $key;
        $form[$field_name][LANGUAGE_NONE][$key]['_weight']['#default_value'] = $key;
      }
    }
    elseif ($field_instance['widget']['type'] == 'entityreference_autocomplete_tags') {
      if (empty($form[$field_name][LANGUAGE_NONE]['#default_value'])) {

        // We use htmlspecialchars() so that apostrophe's are not escaped.
        $form[$field_name][LANGUAGE_NONE]['#default_value'] = htmlspecialchars(implode(', ', $labels));
      }
    }
  }
  elseif ($field_instance['widget']['type'] == 'entityreference_view_widget') {

    // If the field isn't empty, do nothing.
    $children = element_children($form[$field_name][LANGUAGE_NONE]);
    foreach ($children as $child) {
      if (!empty($form[$field_name][LANGUAGE_NONE][$child][$value_key]['#value'])) {
        return;
      }
    }

    // Add a set of checkbox form elements, as the entityreference_view_widget
    // module expects...
    foreach ($entities as $id => $entity) {

      // Add the checkbox element.
      $form[$field_name][LANGUAGE_NONE][$id][$value_key] = array(
        '#type' => 'checkbox',
        '#return_value' => $id,
        '#value' => $id,
        '#title_display' => 'after',
        '#attributes' => array(
          'checked' => 'checked',
        ),
        '#title' => check_plain(entity_label($entity_type, $entity)),
      );
    }
  }
}

Functions

Namesort descending Description
farm_fields_farm_access_perms Implements hook_farm_access_perms().
farm_fields_farm_ui_entities Implements hook_farm_ui_entities().
farm_fields_field_default_field_instances_alter Implements hook_field_default_field_instances_alter().
farm_fields_file_types Returns a list of acceptable file types for image/file fields.
farm_fields_prepopulate_entityreference Helper function for pre-populating entityreference fields in entity forms.