You are here

function _target_string_search in Field formatter conditions 7

Helper function which converts any objects in the target into arrays and compares strings recursively and returns TRUE if the string was found or FALSE if not.

2 calls to _target_string_search()
ffc_condition_execute_hide_if_string in ./ffc.module
Hide the source field when target field contains a string.
ffc_condition_execute_hide_no_string in ./ffc.module
Hide the source field when target field does not contain a string.

File

./ffc.module, line 279
Field formatter conditions.

Code

function _target_string_search(&$build, $configuration, $context = NULL) {

  // Only search if the field is not empty.
  if (!empty($context['entity']->{$configuration}['target']) || !empty($build[$configuration['target']]['#items'])) {

    // Check if we have $context and use it if set, otherwise use $build.
    $target = isset($context) ? $context['entity']->{$configuration}['target'] : $build[$configuration['target']]['#items'];

    // Convert any Objects into Arrays in our target field.
    $converted_target = json_decode(json_encode($target), TRUE);
    $string = $configuration['string'];
    $found = FALSE;

    // Make a little object with things we'll pass into our little lambda below.
    $data = (object) array(
      'string' => $string,
      'found' => &$found,
    );

    // Process each item in this field.
    foreach ($converted_target as $delta) {

      // Whether this item is ready value, or another multidimensional array compare our string.
      array_walk_recursive($delta, create_function('&$item, $key, $data', 'if ($item == $data->string) $data->found = TRUE;'), $data);
    }
    return $found;
  }
  return FALSE;
}