You are here

function fasttoggle_field_merge_field_labels in Fasttoggle 7

Get the labels for this entity type.

Parameters

string $entity_type: The type of entity (node, user...) for which labels are needed.

string $group: The group of settings.

string $fieldname: The name of the field for which labels are being retrieved.

array $field: The field definition.

array $returning: The array to which the result should be added.

1 call to fasttoggle_field_merge_field_labels()
fasttoggle_field_fasttoggle_available_links in module/fasttoggle_field/fasttoggle_field.module
Implements hook_fasttoggle_available_links().

File

module/fasttoggle_field/fasttoggle_field.module, line 55

Code

function fasttoggle_field_merge_field_labels($entity_type, $group, $fieldname, array $field, array &$returning) {
  $result = array();
  $field_info = field_info_field($field['field_name']);
  if ($field_info['module'] == 'list') {
    $new_data = array(
      'description' => empty($field['label']) ? t('%name', array(
        '%name' => $field_info['field_name'],
      )) : t('The field %label in a %entity_type of type %instance_type', array(
        '%label' => $field['label'],
        '%entity_type' => $entity_type,
        '%instance_type' => $group,
      )),
      'access' => array(
        'fasttoggle_fasttoggle_field_access',
      ),
      'default' => isset($field['default_value']) ? $field['default_value'][0]['value'] : FALSE,
      'optional' => $field['required'] == 0,
      'type' => $field_info['type'],
      'content_type' => $group,
      'field_name' => $field_info['field_name'],
      'labels' => array(
        FASTTOGGLE_LABEL_ACTION => array(),
        FASTTOGGLE_LABEL_STATUS => array(),
      ),
    );
    $labels_to_use = array();
    $keys = array_keys($field_info['settings']['allowed_values']);
    foreach (array_keys($keys) as $key) {
      $value = $field_info['settings']['allowed_values'][$keys[$key]];
      $next_index = $key == count($keys) - 1 ? 0 : $key + 1;
      $next_value = $field_info['settings']['allowed_values'][$keys[$next_index]];
      if ($next_index == 0 && !$field['required']) {
        $next_value = 'being unset';
      }
      $new_data['labels'][FASTTOGGLE_LABEL_ACTION][$keys[$key]] = t('Switch to @label', [
        '@label' => $next_value,
      ]);
      $new_data['labels'][FASTTOGGLE_LABEL_STATUS][$keys[$key]] = $value;
    }
    if (!$field['required']) {
      $new_data['labels'][FASTTOGGLE_LABEL_ACTION]['[unset]'] = t('Switch to @label', [
        '@label' => array_shift($field_info['settings']['allowed_values']),
      ]);
      $new_data['labels'][FASTTOGGLE_LABEL_STATUS]['[unset]'] = t('unset');
    }
    $result[$entity_type]['fields'][$group]['instances'][$field_info['field_name']] = $new_data;
  }
  if (isset($result) && is_array($result)) {
    $returning = array_merge_recursive($returning, $result);
  }
  elseif (isset($result)) {
    $returning[] = $result;
  }
}