protected function ContentIndexerTrait::indexContent in Tome 8
Writes content to the index.
Parameters
\Drupal\Core\Entity\ContentEntityInterface $entity: An entity to be indexed.
2 calls to ContentIndexerTrait::indexContent()
- Exporter::exportContent in modules/
tome_sync/ src/ Exporter.php - Exports a content entity to the target storage.
- ImportPartialFormTest::testImportPartialForm in modules/
tome_sync/ tests/ src/ Functional/ ImportPartialFormTest.php - Tests that the import partial form works.
File
- modules/
tome_sync/ src/ ContentIndexerTrait.php, line 27
Class
- ContentIndexerTrait
- Provides methods for reading and writing the index file.
Namespace
Drupal\tome_syncCode
protected function indexContent(ContentEntityInterface $entity) {
// @todo Replace when trait becomes service in Tome 2.x.
$entity_type_manager = \Drupal::entityTypeManager();
$dependencies = [];
foreach ($entity as $field) {
if ($field instanceof EntityReferenceFieldItemListInterface) {
foreach ($field
->referencedEntities() as $referenced_entity) {
if ($referenced_entity instanceof ContentEntityInterface) {
$dependencies[] = TomeSyncHelper::getContentName($referenced_entity);
}
}
}
elseif ($field instanceof FieldItemListInterface) {
foreach ($field as $item) {
/** @var \Drupal\Core\Field\FieldItemInterface $item */
foreach ($item as $property) {
// @see \Drupal\tome_sync\Normalizer\UriNormalizer
if ($property instanceof UriInterface && strpos($property
->getValue(), 'entity:') === 0) {
$parts = explode('/', str_replace('entity:', '', $property
->getValue()));
if (count($parts) >= 2 && $entity_type_manager
->hasDefinition($parts[0]) && is_numeric($parts[1])) {
if ($referenced_entity = $entity_type_manager
->getStorage($parts[0])
->load($parts[1])) {
$dependencies[] = TomeSyncHelper::getContentNameFromParts($referenced_entity
->getEntityTypeId(), $referenced_entity
->uuid());
}
}
}
}
}
}
}
if (!$entity
->isDefaultTranslation()) {
$dependencies[] = TomeSyncHelper::getContentNameFromParts($entity
->getEntityTypeId(), $entity
->uuid());
}
if (is_a($entity, '\\Drupal\\path_alias\\PathAliasInterface')) {
foreach ([
'path',
'alias',
] as $key) {
if (!empty($entity
->get($key)->value)) {
$parts = explode('/', $entity
->get($key)->value);
if (count($parts) >= 3 && $entity_type_manager
->hasDefinition($parts[1]) && is_numeric($parts[2])) {
if ($referenced_entity = $entity_type_manager
->getStorage($parts[1])
->load($parts[2])) {
$dependencies[] = TomeSyncHelper::getContentName($referenced_entity);
}
}
}
}
}
$handle = $this
->acquireContentIndexLock();
$contents = stream_get_contents($handle);
if (empty($contents)) {
$index = [];
}
else {
$index = json_decode($contents, TRUE);
}
$dependencies = array_values(array_unique($dependencies));
$index[TomeSyncHelper::getContentName($entity)] = $dependencies;
ftruncate($handle, 0);
rewind($handle);
fwrite($handle, json_encode($index, JSON_PRETTY_PRINT));
flock($handle, LOCK_UN);
}