You are here

public function HideIfEmpty::access in Field Formatter Condition 8

Access control function.

Parameters

array $build: The current build array.

string $field: The current field name.

array $settings: The current settings array.

Overrides FieldFormatterConditionBase::access

File

src/Plugin/Field/FieldFormatter/Condition/HideIfEmpty.php, line 53

Class

HideIfEmpty
The plugin for check empty fields.

Namespace

Drupal\fico\Plugin\Field\FieldFormatter\Condition

Code

public function access(&$build, $field, $settings) {
  if (isset($build[$settings['settings']['target_field']]['#items'])) {
    $fields = $build[$settings['settings']['target_field']]['#items'];
    if (is_object($fields)) {
      $field_storage = FieldStorageConfig::loadByName($settings['entity_type'], $settings['settings']['target_field']);
      $values = $fields
        ->getValue();
      switch ($field_storage
        ->getType()) {
        case 'comment':
          if ($values[0]['comment_count'] == 0) {
            $build[$field]['#access'] = FALSE;
          }
          break;
        case 'image':
        case 'entity_reference':
          if (!isset($values[0]['target_id'])) {
            $build[$field]['#access'] = FALSE;
          }
          break;
        case 'link':
          if (!isset($values[0]['uri'])) {
            $build[$field]['#access'] = FALSE;
          }
          break;
        default:
          if (!isset($values[0]['value'])) {
            $build[$field]['#access'] = FALSE;
          }
      }
    }
  }
  else {
    if ($entity = $this
      ->getEntity($build)) {
      if ($entity
        ->get($settings['settings']['target_field'])
        ->isEmpty()) {
        $build[$field]['#access'] = FALSE;
      }
    }
    else {
      $build[$field]['#access'] = FALSE;
    }
  }
}