Merge branch 'farm_flags' into 7.x-1.x

This commit is contained in:
Michael Stenta 2018-04-16 20:26:20 -04:00
commit a04342611c
3 changed files with 267 additions and 0 deletions

View File

@ -36,6 +36,16 @@ function hook_farm_flags() {
);
}
/**
* Allow modules to alter the classes that are added to flags when they are
* displayed in farmOS.
*/
function hook_farm_flags_classes_alter($flag, &$classes) {
if ($flag == 'priority') {
$classes[] = 'my-priority-class';
}
}
/**
* @}
*/

View File

@ -29,6 +29,236 @@ function farm_flags_field_allowed_values() {
function farm_flags_farm_flags() {
return array(
'priority' => t('Priority'),
'monitor' => t('Monitor'),
'review' => t('Needs Review'),
);
}
/**
* 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 $entity
* The entity object.
* @param array $context
* Array with parameters for this action.
*/
function farm_flags_action(Entity $entity, $context = array()) {
// If the operation is invalid, bail.
if (!in_array($context['operation'], array('append', 'replace'))) {
drupal_set_message('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();
}
}
// Or, if we are replacing, clear out the existing flags.
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();
}
}

View File

@ -228,6 +228,7 @@ function farm_theme_views_bulk_operations_form_alter(&$form, &$form_state, $vbo)
function farm_theme_bootstrap_colorize_text_alter(&$texts) {
// Colorize VBO action buttons.
$texts['matches'][t('Flag')] = 'info';
$texts['matches'][t('Move')] = 'success';
$texts['matches'][t('Group')] = 'warning';
$texts['matches'][t('Done')] = 'success';
@ -246,6 +247,7 @@ function farm_theme_bootstrap_colorize_text_alter(&$texts) {
function farm_theme_bootstrap_iconize_text_alter(&$texts) {
// Iconize VBO action buttons.
$texts['matches'][t('Flag')] = 'flag';
$texts['matches'][t('Move')] = 'globe';
$texts['matches'][t('Group')] = 'bookmark';
$texts['matches'][t('Done')] = 'check';
@ -258,6 +260,31 @@ function farm_theme_bootstrap_iconize_text_alter(&$texts) {
$texts['matches'][t('Delete')] = 'trash';
}
/**
* Implements hook_farm_flags_classes_alter().
*/
function farm_theme_farm_flags_classes_alter($flag, &$classes) {
// Render all flags as extra small buttons using Bootstrap's classes.
$classes[] = 'btn';
$classes[] = 'btn-xs';
// Add a button style class based on the flag used.
switch ($flag) {
case 'priority':
$classes[] = 'btn-primary';
break;
case 'monitor':
$classes[] = 'btn-danger';
break;
case 'review':
$classes[] = 'btn-warning';
break;
default:
$classes[] = 'btn-default';
}
}
/**
* Implements hook_entity_view_alter().
*/