farm_flags.module in farmOS 7
Farm flags module.
File
modules/farm/farm_flags/farm_flags.moduleView source
<?php
/**
* @file
* Farm flags module.
*/
/**
* Get a list of available flags.
*/
function farm_flags_list() {
// Ask modules for flags.
$flags = module_invoke_all('farm_flags');
// Return them.
return $flags;
}
/**
* Allowed values callback function for the flags field.
*/
function farm_flags_field_allowed_values() {
return farm_flags_list();
}
/**
* Implements hook_farm_flags().
*/
function farm_flags_farm_flags() {
return array(
'priority' => t('Priority'),
'monitor' => t('Monitor'),
'review' => t('Needs Review'),
);
}
/**
* Helper function for loading a list of flags on an entity.
*
* @param $entity
* The entity with flags on it (area, asset, or log).
*
* @return array
* Returns an array of flag machine names.
*/
function farm_flags_load($entity) {
// Start with an empty array.
$flags = array();
// If there are flags, iterate through them and add them to the array.
if (!empty($entity->field_farm_flags[LANGUAGE_NONE])) {
foreach ($entity->field_farm_flags[LANGUAGE_NONE] as $value) {
if (!empty($value['value'])) {
$flags[] = $value['value'];
}
}
}
// Return the flags.
return $flags;
}
/**
* Implements hook_preprocess_field().
*/
function farm_flags_preprocess_field(&$variables, $hook) {
// Only act on field_farm_flags.
if (empty($variables['element']['#field_name']) || $variables['element']['#field_name'] != 'field_farm_flags') {
return;
}
// Wrap the flag in a span with a class.
if (!empty($variables['element']['#items'])) {
foreach ($variables['element']['#items'] as $key => $item) {
if (!empty($variables['items'][$key]['#markup'])) {
$string = $variables['items'][$key]['#markup'];
$class = $item['value'];
$variables['items'][$key]['#markup'] = farm_flags_wrap($string, $class);
}
}
}
}
/**
* Implements hook_views_pre_render().
*/
function farm_flags_views_pre_render(&$view) {
// If there are no results, bail early.
if (empty($view->result)) {
return;
}
// Iterate through results.
foreach ($view->result as $item) {
// If there are no flags, skip it.
if (empty($item->field_field_farm_flags)) {
continue;
}
// Wrap flags in a span with a class.
foreach ($item->field_field_farm_flags as &$flag) {
$string = $flag['rendered']['#markup'];
$class = $flag['raw']['value'];
$flag['rendered']['#markup'] = farm_flags_wrap($string, $class);
}
}
}
/**
* Wrap a flag string in a span.
*
* @param $string
* The string to wrap.
* @param $flag
* The flag machine name.
*
* @return string
* Returns the string wrapped in a span.
*/
function farm_flags_wrap($string, $flag) {
// Start an array of classes.
$classes = array();
// Add the flag itself as a class.
$classes[] = check_plain($flag);
// Allow other modules to alter the classes, or add their own.
drupal_alter('farm_flags_classes', $flag, $classes);
// Wrap the string and return it.
return '<span class="' . implode(' ', $classes) . '">' . $string . '</span>';
}
/**
* Implements hook_action_info().
*/
function farm_flags_action_info() {
return array(
'farm_flags_action' => array(
'type' => 'entity',
'label' => t('Flag'),
'configurable' => TRUE,
'triggers' => array(
'any',
),
),
);
}
/**
* Configuration form for farm_flags_action.
*
* @param array $context
* The context passed into the action form function.
* @param array $form_state
* The form state passed into the action form function.
*
* @return array
* Returns a form array.
*/
function farm_flags_action_form(array $context, array $form_state) {
// Store the entity type in the form values.
$form['entity_type'] = array(
'#type' => 'value',
'#value' => $context['entity_type'],
);
// Get a list of flag options.
$flag_options = farm_flags_field_allowed_values();
// Display a multi-select list.
$form['flags'] = array(
'#type' => 'select',
'#title' => t('Flags'),
'#description' => t('Select the flags that should be attached to the record(s).'),
'#options' => $flag_options,
'#multiple' => TRUE,
);
// Add a checkbox for appending the flags instead of overwriting them.
$form['operation'] = array(
'#type' => 'radios',
'#title' => t('Append or Replace'),
'#description' => t('Select "Append" if you want to add flags to the records, but keep existing flags. Select "Replace" if you want to replace existing flags with the ones specified above.'),
'#options' => array(
'append' => t('Append'),
'replace' => t('Replace'),
),
'#default_value' => 'append',
);
// Return the form.
return $form;
}
/**
* Configuration form submit for farm_flags_action.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*
* @return array
* Returns an array that will end up in the action's context.
*/
function farm_flags_action_submit(array $form, array $form_state) {
return array(
'entity_type' => $form_state['values']['entity_type'],
'flags' => $form_state['values']['flags'],
'operation' => $form_state['values']['operation'],
);
}
/**
* Action function for farm_flags_action.
*
* Assigns a log to one or more people.
*
* @param $entity
* The entity object.
* @param array $context
* Array with parameters for this action.
*/
function farm_flags_action($entity, $context = array()) {
// If the operation is invalid, bail.
if (!in_array($context['operation'], array(
'append',
'replace',
))) {
drupal_set_message(t('Invalid operation.'));
return;
}
// If the operation is 'append', and there are no flags, bail.
if ($context['operation'] == 'append' && empty($context['flags'])) {
return;
}
// Create an entity wrapper object.
$entity_wrapper = entity_metadata_wrapper($context['entity_type'], $entity);
// If the flags field doesn't exist, bail.
if (!isset($entity_wrapper->field_farm_flags)) {
return;
}
// Keep track of flags that are already assigned.
$existing_flags = array();
// If we are appending, load existing flags.
if ($context['operation'] == 'append' && !empty($entity_wrapper->field_farm_flags)) {
foreach ($entity_wrapper->field_farm_flags
->getIterator() as $delta => $flag) {
$existing_flags[] = $flag
->value();
}
}
elseif ($context['operation'] == 'replace') {
$entity_wrapper->field_farm_flags = array();
}
// Assume that we are not going to save the entity.
$save = FALSE;
// Iterate through the flags.
foreach ($context['flags'] as $flag) {
// If the flag is already referenced in the entity, skip it.
if (in_array($flag, $existing_flags)) {
continue;
}
// Add the flag to the array of existing flags so we don't accidentally
// add the same one more than once. Shouldn't happen, but be defensive.
$existing_flags[] = $flag;
// Add the flag to the entity's flags field.
$entity_wrapper->field_farm_flags[] = $flag;
// We will save the entity.
$save = TRUE;
}
// If we should save the entity, then save it.
if ($save) {
$entity_wrapper
->save();
}
}
Functions
Name | Description |
---|---|
farm_flags_action | Action function for farm_flags_action. |
farm_flags_action_form | Configuration form for farm_flags_action. |
farm_flags_action_info | Implements hook_action_info(). |
farm_flags_action_submit | Configuration form submit for farm_flags_action. |
farm_flags_farm_flags | Implements hook_farm_flags(). |
farm_flags_field_allowed_values | Allowed values callback function for the flags field. |
farm_flags_list | Get a list of available flags. |
farm_flags_load | Helper function for loading a list of flags on an entity. |
farm_flags_preprocess_field | Implements hook_preprocess_field(). |
farm_flags_views_pre_render | Implements hook_views_pre_render(). |
farm_flags_wrap | Wrap a flag string in a span. |