You are here

public function RenderedItem::addFieldValues in Search API 8

Adds the values of properties defined by this processor to the item.

Parameters

\Drupal\search_api\Item\ItemInterface $item: The item whose field values should be added.

Overrides ProcessorPluginBase::addFieldValues

File

src/Plugin/search_api/processor/RenderedItem.php, line 233

Class

RenderedItem
Adds an additional field containing the rendered item.

Namespace

Drupal\search_api\Plugin\search_api\processor

Code

public function addFieldValues(ItemInterface $item) {

  // Switch to the default theme in case the admin theme is enabled.
  $active_theme = $this
    ->getThemeManager()
    ->getActiveTheme();
  $default_theme = $this
    ->getConfigFactory()
    ->get('system.theme')
    ->get('default');
  $default_theme = $this
    ->getThemeInitializer()
    ->getActiveThemeByName($default_theme);
  $this
    ->getThemeManager()
    ->setActiveTheme($default_theme);

  // Fields for which some view mode config is missing.
  $unset_view_modes = [];
  $fields = $this
    ->getFieldsHelper()
    ->filterForPropertyPath($item
    ->getFields(), NULL, 'rendered_item');
  foreach ($fields as $field) {
    $configuration = $field
      ->getConfiguration();

    // Change the current user to our dummy implementation to ensure we are
    // using the configured roles.
    $this
      ->getAccountSwitcher()
      ->switchTo(new UserSession([
      'roles' => $configuration['roles'],
    ]));
    $datasource_id = $item
      ->getDatasourceId();
    $datasource = $item
      ->getDatasource();
    $bundle = $datasource
      ->getItemBundle($item
      ->getOriginalObject());

    // When no view mode has been set for the bundle, or it has been set to
    // "Don't include the rendered item", skip this item.
    if (empty($configuration['view_mode'][$datasource_id][$bundle])) {

      // If it was really not set, also notify the user through the log.
      if (!isset($configuration['view_mode'][$datasource_id][$bundle])) {
        $unset_view_modes[$field
          ->getFieldIdentifier()] = $field
          ->getLabel();
      }
      continue;
    }
    $view_mode = (string) $configuration['view_mode'][$datasource_id][$bundle];
    try {
      $build = $datasource
        ->viewItem($item
        ->getOriginalObject(), $view_mode);
      if ($build) {

        // Add the excerpt to the render array to allow adding it to view modes.
        $build['#search_api_excerpt'] = $item
          ->getExcerpt();
        $value = (string) $this
          ->getRenderer()
          ->renderPlain($build);
        if ($value) {
          $field
            ->addValue($value);
        }
      }
    } catch (\Exception $e) {

      // This could throw all kinds of exceptions in specific scenarios, so we
      // just catch all of them here. Not having a field value for this field
      // probably makes sense in that case, so we just log an error and
      // continue.
      $variables = [
        '%item_id' => $item
          ->getId(),
        '%view_mode' => $view_mode,
        '%index' => $this->index
          ->label(),
      ];
      $this
        ->logException($e, '%type while trying to render item %item_id with view mode %view_mode for search index %index: @message in %function (line %line of %file).', $variables);
    }
  }

  // Restore the original user.
  $this
    ->getAccountSwitcher()
    ->switchBack();

  // Restore the original theme.
  $this
    ->getThemeManager()
    ->setActiveTheme($active_theme);
  if ($unset_view_modes > 0) {
    foreach ($unset_view_modes as $field_id => $field_label) {
      $url = new Url('entity.search_api_index.field_config', [
        'search_api_index' => $this->index
          ->id(),
        'field_id' => $field_id,
      ]);
      $context = [
        '%index' => $this->index
          ->label(),
        '%field_id' => $field_id,
        '%field_label' => $field_label,
        'link' => (new Link($this
          ->t('Field settings'), $url))
          ->toString(),
      ];
      $this
        ->getLogger()
        ->warning('The field %field_label (%field_id) on index %index is missing view mode configuration for some datasources or bundles. Please review (and re-save) the field settings.', $context);
    }
  }
}