You are here

function fico_string_search in Field Formatter Condition 8

Search a string in a field..

Parameters

array $build: The current build array.

string $field: The current field name.

array $settings: The current settings array.

2 calls to fico_string_search()
HideIfString::access in src/Plugin/Field/FieldFormatter/Condition/HideIfString.php
Access control function.
HideNoString::access in src/Plugin/Field/FieldFormatter/Condition/HideNoString.php
Access control function.

File

./fico.module, line 346
Contains fico.module.

Code

function fico_string_search(&$build, $field, $settings) {
  if (!empty($settings['settings']['target_field']) && !empty($build[$settings['settings']['target_field']]['#items'])) {
    $field_storage = FieldStorageConfig::loadByName($settings['entity_type'], $settings['settings']['target_field']);
    $target = $build[$settings['settings']['target_field']]['#items'];
    $values = $target
      ->getValue($field);
    $found = FALSE;
    $options = [
      'found' => &$found,
      'string' => $settings['settings']['string'],
    ];
    switch ($field_storage
      ->getType()) {
      case 'list_string':
        $values = $field_storage
          ->getSettings()['allowed_values'];
        if (in_array($settings['settings']['string'], array_keys($values))) {
          return TRUE;
        }
        break;
      default:
        array_walk($values, function (&$item, $delta, $options) {
          if (!empty($item['value']) && strpos($item['value'], $options['string']) !== FALSE) {
            $options['found'] = TRUE;
          }
        }, $options);
    }
    return $found;
  }
}