You are here

farm_equipment_field.module in farmOS 7

File

modules/farm/farm_equipment/farm_equipment_field/farm_equipment_field.module
View source
<?php

/**
 * @file
 * Farm equipment field module.
 */
include_once 'farm_equipment_field.features.inc';

/**
 * Implements hook_farm_fields_dynamic_bases().
 */
function farm_equipment_farm_fields_dynamic_bases() {
  return array(
    'field_farm_equipment' => array(
      'active' => 1,
      'cardinality' => -1,
      'deleted' => 0,
      'entity_types' => array(),
      'field_name' => 'field_farm_equipment',
      'indexes' => array(
        'target_id' => array(
          0 => 'target_id',
        ),
      ),
      'locked' => 0,
      'module' => 'entityreference',
      'settings' => array(
        'handler' => 'base',
        'handler_settings' => array(
          'behaviors' => array(
            'views-select-list' => array(
              'status' => 0,
            ),
          ),
          'sort' => array(
            'type' => 'none',
          ),
          'target_bundles' => array(
            0 => 'equipment',
          ),
        ),
        'target_type' => 'farm_asset',
      ),
      'translatable' => 0,
      'type' => 'entityreference',
    ),
  );
}

/**
 * Implements hook_farm_fields_dynamic_instances().
 */
function farm_equipment_farm_fields_dynamic_instances() {

  // Start an empty array of field instance definitions.
  $field_instances = array();

  // Get a list of log types.
  $log_types = log_types();

  // Iterate through the log types and add an equipment reference field to each.
  foreach ($log_types as $bundle => $info) {
    $field_instances['log-' . $bundle . '-field_farm_equipment'] = array(
      'bundle' => $bundle,
      'deleted' => 0,
      'description' => 'What equipment was used?',
      'display' => array(
        'default' => array(
          'label' => 'inline',
          'module' => 'entityreference',
          'settings' => array(
            'bypass_access' => FALSE,
            'link' => 1,
          ),
          'type' => 'entityreference_label',
          'weight' => 0,
        ),
      ),
      'entity_type' => 'log',
      'field_name' => 'field_farm_equipment',
      'label' => t('Equipment used'),
      'required' => 0,
      'settings' => array(
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'active' => 1,
        'module' => 'entityreference_view_widget',
        'settings' => array(
          'allow_duplicates' => 0,
          'close_modal' => 1,
          'pass_argument' => 1,
          'pass_arguments' => '',
          'rendered_entity' => 0,
          'view' => 'farm_equipment_entityreference_view|entityreference_view_widget',
          'view_mode' => 'full',
        ),
        'type' => 'entityreference_view_widget',
        'weight' => 2,
      ),
    );
  }

  // Return the array of field instance definitions.
  return $field_instances;
}

/**
 * Implements hook_ctools_plugin_api().
 */
function farm_equipment_field_ctools_plugin_api($module = NULL, $api = NULL) {
  if ($module == "field_group" && $api == "field_group") {
    return array(
      "version" => "1",
    );
  }
}

/**
 * Implements hook_field_group_info().
 */
function farm_equipment_field_field_group_info() {
  $field_groups = array();

  // Get a list of log types.
  $log_types = log_types();

  // Iterate through the log types and add an equipment field group to each.
  foreach ($log_types as $bundle => $info) {
    $group_name = 'group_farm_equipment';
    $entity_type = 'log';
    $field_group = new stdClass();
    $field_group->disabled = FALSE;

    /* Edit this to true to make a default field_group disabled initially */
    $field_group->api_version = 1;
    $field_group->identifier = $group_name . '|' . $entity_type . '|' . $bundle . '|form';
    $field_group->group_name = $group_name;
    $field_group->entity_type = $entity_type;
    $field_group->bundle = $bundle;
    $field_group->mode = 'form';
    $field_group->parent_name = 'group_farm_tabs';
    $field_group->data = array(
      'label' => t('Equipment'),
      'weight' => '3',
      'children' => array(
        0 => 'field_farm_equipment',
      ),
      'format_type' => 'fieldset',
      'format_settings' => array(
        'label' => t('Equipment'),
        'instance_settings' => array(
          'required_fields' => 1,
          'id' => '',
          'classes' => 'group-farm-assets field-group-fieldset',
          'description' => '',
        ),
        'formatter' => 'open',
      ),
    );
    $field_groups[$group_name . '|' . $entity_type . '|' . $bundle . '|form'] = $field_group;
  }
  return $field_groups;
}

/**
 * Implements hook_field_group_info_alter().
 */
function farm_equipment_field_field_group_info_alter(&$groups) {

  // Iterate through the groups.
  foreach ($groups as $key => $group) {

    // If we don't know the entity type, bail.
    if (empty($group->entity_type)) {
      continue;
    }

    // Only alter logs.
    if ($group->entity_type != 'log') {
      continue;
    }

    // Only alter the log form.
    if ($group->mode != 'form') {
      continue;
    }

    // Only alter the group_farm_tabs group.
    if ($group->group_name != 'group_farm_tabs') {
      continue;
    }

    // Add the "Equipment" group.
    $groups[$key]->data['children'][] = 'group_farm_equipment';
  }
}

/**
 * Implements hook_farm_ui_entity_view_groups().
 */
function farm_equipment_field_farm_ui_entity_view_groups() {
  $groups = array(
    'equipment' => array(
      'title' => t('Equipment'),
      'weight' => 90,
    ),
  );
  return $groups;
}

/**
 * Implements hook_farm_ui_entity_views().
 */
function farm_equipment_field_farm_ui_entity_views($entity_type, $bundle, $entity) {
  $views = array();

  // If the entity is an equipment asset, add equipment use logs.
  if ($entity_type == 'farm_asset' && $bundle == 'equipment') {
    $views[] = array(
      'name' => 'farm_equipment_log',
      'group' => 'equipment',
      'always' => TRUE,
    );
  }
  return $views;
}