You are here

function fico_entity_view_alter in Field Formatter Condition 8

Implements hook_entity_view_alter().

File

./fico.module, line 260
Contains fico.module.

Code

function fico_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  static $loaded = FALSE;
  $components = $display
    ->getComponents();
  $entity_type = $entity
    ->getEntityTypeId();
  $bundle = $entity
    ->bundle();
  $view_mode = $display
    ->getMode();
  foreach ($components as $field => $component) {
    if (!empty($component['third_party_settings']['fico']['fico']['condition'])) {
      $settings = [
        'condition' => $component['third_party_settings']['fico']['fico']['condition'],
        'entity_type' => $entity
          ->getEntityTypeId(),
        'bundle' => $entity
          ->bundle(),
        'settings' => !empty($component['third_party_settings']['fico']['fico']['settings']) ? $component['third_party_settings']['fico']['fico']['settings'] : NULL,
        'field_name' => $field,
        'field_type' => $component['type'],
      ];
      $fico_plugin_manager = Drupal::service('plugin.manager.field_formatter_condition');
      $formatterPlugin = $fico_plugin_manager
        ->createInstance($settings['condition']);
      $formatterPlugin
        ->access($build, $field, $settings);
    }
  }

  // If Display Suite is disabled, stop here.
  if (Ds::isDisabled()) {
    return;
  }

  // Load field permissions and layouts only once.
  if (!$loaded) {
    $loaded = TRUE;

    #$field_permissions = \Drupal::config('ds_extras.settings')->get('field_permissions');
  }
  $configuration = $display
    ->getThirdPartySettings('ds');
  $fields = Ds::getFields($entity_type);
  $field_values = !empty($configuration['fields']) ? $configuration['fields'] : array();
  if (isset($configuration['regions'])) {
    foreach ($configuration['regions'] as $region_name => $region) {
      foreach ($region as $weight => $key) {

        // Ignore if this field is not a DS field, just pull it in from the
        // entity.
        if (!isset($fields[$key])) {
          continue;
        }
        $field = $fields[$key];

        /* @var $field_instance \Drupal\ds\Plugin\DsField\DsFieldInterface */
        $field_instance = Ds::getFieldInstance($key, $field, $entity, $view_mode, $display, $build);
        $field_value = $field_instance
          ->build();
        $field_title = $field_instance
          ->getTitle();

        // Only allow non empty fields.
        if (!empty($field_value)) {
          $pluginSettings = $field_instance
            ->getConfiguration();
          if (!empty($pluginSettings['fico']['condition'])) {
            $settings = [
              'condition' => $pluginSettings['fico']['condition'],
              'entity_type' => $entity
                ->getEntityTypeId(),
              'bundle' => $entity
                ->bundle(),
              'settings' => !empty($pluginSettings['fico']['settings']) ? $pluginSettings['fico']['settings'] : NULL,
              'field_name' => $key,
              'field_type' => 'ds',
            ];
            $fico_plugin_manager = Drupal::service('plugin.manager.field_formatter_condition');
            $formatterPlugin = $fico_plugin_manager
              ->createInstance($pluginSettings['fico']['condition']);
            $formatterPlugin
              ->access($build, $key, $settings);
          }
        }
      }
    }
  }
}