You are here

public function ViewsNaturalSortService::storeIndexRecordsFromEntity in Views Natural Sort 8.2

File

src/ViewsNaturalSortService.php, line 200

Class

ViewsNaturalSortService
Service that manages Views Natural Sort records.

Namespace

Drupal\views_natural_sort

Code

public function storeIndexRecordsFromEntity(EntityInterface $entity) {

  // TODO: Consider abstracting this out. The creation and storage of records
  // should be handled by a converter class that interacts with specific
  // IndexRecordTypes and creates IndexRecords. Those would probably be called
  // directly and have nothign to do with this service.
  $entity_type = $entity
    ->getEntityTypeId();
  $supported_entity_properties = $this
    ->getViewsSupportedEntityProperties();
  foreach ($supported_entity_properties[$entity_type] as $field => $field_info) {
    if (!isset($entity->{$field})) {
      continue;
    }
    foreach ($entity
      ->get($field)
      ->getValue() as $delta => $value) {
      $record = $this
        ->createIndexRecord([
        'eid' => $entity
          ->id(),
        'entity_type' => $entity_type,
        'field' => $field,
        'delta' => $delta,
        // This may have to be passed in if it's not always ['value'].
        'content' => $value['value'],
      ]);
      $record
        ->save();
    }
  }
}