Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-5905: Implemented LoadContent events #250

Open
wants to merge 11 commits into
base: 4.6
Choose a base branch
from
7 changes: 7 additions & 0 deletions src/contracts/Persistence/Content/ContentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class ContentInfo extends ValueObject
*/
public $contentTypeId;

public string $contentTypeIdentifier;

/**
* Section id the content is assigned to.
*
Expand Down Expand Up @@ -134,6 +136,11 @@ class ContentInfo extends ValueObject
* @var bool
*/
public $isHidden = false;

public function getContentTypeIdentifier(): string
{
return $this->contentTypeIdentifier;
}
}

class_alias(ContentInfo::class, 'eZ\Publish\SPI\Persistence\Content\ContentInfo');
90 changes: 90 additions & 0 deletions src/contracts/Repository/Events/Content/BeforeLoadContentEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Events\Content;

use Ibexa\Contracts\Core\Repository\Event\BeforeEvent;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use UnexpectedValueException;

final class BeforeLoadContentEvent extends BeforeEvent
{
private int $contentId;

/** @var string[]|null */
private ?array $languages;

private ?int $versionNo;

private bool $useAlwaysAvailable;

private ?Content $content = null;

/**
* @param string[] $languages
*/
public function __construct(
int $contentId,
?array $languages = null,
?int $versionNo = null,
bool $useAlwaysAvailable = true
) {
$this->contentId = $contentId;
$this->languages = $languages;
$this->versionNo = $versionNo;
$this->useAlwaysAvailable = $useAlwaysAvailable;
}

public function getContentId(): int
{
return $this->contentId;
}

/**
* @return string[]|null
*/
public function getLanguages(): ?array
{
return $this->languages;
}

public function getVersionNo(): ?int
{
return $this->versionNo;
}

public function getUseAlwaysAvailable(): bool
{
return $this->useAlwaysAvailable;
}

public function getContent(): Content
{
if (!$this->hasContent()) {
throw new UnexpectedValueException(
sprintf(
'Return value is not set or not of type %s. Check hasContent() or set it using setContent() before you call the getter.',
Content::class
)
);
}

return $this->content;
}

public function setContent(?Content $content): void
{
$this->content = $content;
}

/** @phpstan-assert-if-true !null $this->content */
public function hasContent(): bool
{
return $this->content instanceof Content;
}
}
71 changes: 71 additions & 0 deletions src/contracts/Repository/Events/Content/LoadContentEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Events\Content;

use Ibexa\Contracts\Core\Repository\Event\AfterEvent;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;

final class LoadContentEvent extends AfterEvent
{
private int $contentId;

private Content $content;

/** @var string[]|null */
private ?array $languages;

private ?int $versionNo;

private bool $useAlwaysAvailable;

/**
* @param string[] $languages
*/
public function __construct(
Content $content,
int $contentId,
?array $languages = null,
?int $versionNo = null,
bool $useAlwaysAvailable = true
) {
$this->contentId = $contentId;
$this->content = $content;
$this->languages = $languages;
$this->versionNo = $versionNo;
$this->useAlwaysAvailable = $useAlwaysAvailable;
}

public function getContent(): Content
{
return $this->content;
}

/**
* @return string[]|null
*/
public function getLanguages(): ?array
{
return $this->languages;
}

public function getVersionNo(): ?int
{
return $this->versionNo;
}

public function getUseAlwaysAvailable(): bool
{
return $this->useAlwaysAvailable;
}

public function getContentId(): int
{
return $this->contentId;
}
}
33 changes: 33 additions & 0 deletions src/lib/Event/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Ibexa\Contracts\Core\Repository\Events\Content\BeforeDeleteTranslationEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\BeforeDeleteVersionEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\BeforeHideContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\BeforeLoadContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\BeforePublishVersionEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\BeforeRevealContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\BeforeUpdateContentEvent;
Expand All @@ -32,6 +33,7 @@
use Ibexa\Contracts\Core\Repository\Events\Content\DeleteTranslationEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\DeleteVersionEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\HideContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\LoadContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\PublishVersionEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\RevealContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\UpdateContentEvent;
Expand Down Expand Up @@ -380,6 +382,37 @@ public function revealContent(ContentInfo $contentInfo): void
new RevealContentEvent(...$eventData)
);
}

public function loadContent(
int $contentId,
?array $languages = null,
?int $versionNo = null,
bool $useAlwaysAvailable = true
): Content {
$eventData = [
$contentId,
$languages,
$versionNo,
$useAlwaysAvailable,
];

$beforeEvent = new BeforeLoadContentEvent(...$eventData);

$this->eventDispatcher->dispatch($beforeEvent);
if ($beforeEvent->isPropagationStopped()) {
return $beforeEvent->getContent();
}

$content = $beforeEvent->hasContent()
? $beforeEvent->getContent()
: $this->innerService->loadContent($contentId, $languages, $versionNo, $useAlwaysAvailable);

$this->eventDispatcher->dispatch(
new LoadContentEvent($content, ...$eventData)
);

return $content;
}
}

class_alias(ContentService::class, 'eZ\Publish\Core\Event\ContentService');
19 changes: 15 additions & 4 deletions src/lib/Persistence/Legacy/Content/Gateway/DoctrineDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Ibexa\Contracts\Core\Persistence\Content\Language\Handler as LanguageHandler;
use Ibexa\Contracts\Core\Persistence\Content\MetadataUpdateStruct;
use Ibexa\Contracts\Core\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
use Ibexa\Contracts\Core\Persistence\Content\Type;
use Ibexa\Contracts\Core\Persistence\Content\UpdateStruct;
use Ibexa\Contracts\Core\Persistence\Content\VersionInfo;
use Ibexa\Contracts\Core\Repository\Values\Content\Relation;
Expand Down Expand Up @@ -769,14 +770,15 @@ private function internalLoadContent(
'a.data_text AS ezcontentobject_attribute_data_text',
'a.sort_key_int AS ezcontentobject_attribute_sort_key_int',
'a.sort_key_string AS ezcontentobject_attribute_sort_key_string',
't.main_node_id AS ezcontentobject_tree_main_node_id'
't.main_node_id AS ezcontentobject_tree_main_node_id',
'ct.identifier AS content_type_identifier',
)
->from('ezcontentobject', 'c')
->innerJoin(
'c',
'ezcontentobject_version',
'v',
$expr->andX(
$expr->and(
$expr->eq('c.id', 'v.contentobject_id'),
$expr->eq('v.version', $version ?? 'c.current_version')
)
Expand All @@ -785,7 +787,7 @@ private function internalLoadContent(
'v',
'ezcontentobject_attribute',
'a',
$expr->andX(
$expr->and(
$expr->eq('v.contentobject_id', 'a.contentobject_id'),
$expr->eq('v.version', 'a.version')
)
Expand All @@ -794,10 +796,19 @@ private function internalLoadContent(
'c',
'ezcontentobject_tree',
't',
$expr->andX(
$expr->and(
$expr->eq('c.id', 't.contentobject_id'),
$expr->eq('t.node_id', 't.main_node_id')
)
)
->innerJoin(
'c',
'ezcontentclass',
'ct',
$expr->and(
$expr->eq('c.contentclass_id', 'ct.id'),
$expr->eq('ct.version', Type::STATUS_DEFINED)
)
);

$queryBuilder->where(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\QueryBuilder as DoctrineQueryBuilder;
use Ibexa\Contracts\Core\Persistence\Content\Type;
use Ibexa\Core\Persistence\Legacy\Content\Gateway;
use function time;

Expand Down Expand Up @@ -113,13 +114,26 @@ public function createLoadContentInfoQueryBuilder(
}

$queryBuilder
->select('c.*', 't.main_node_id AS ezcontentobject_tree_main_node_id')
->select(
'c.*',
't.main_node_id AS ezcontentobject_tree_main_node_id',
'ct.identifier AS content_type_identifier'
)
->from(Gateway::CONTENT_ITEM_TABLE, 'c')
->leftJoin(
'c',
'ezcontentobject_tree',
't',
$joinCondition
)
->innerJoin(
'c',
'ezcontentclass',
'ct',
$expr->and(
$expr->eq('c.contentclass_id', 'ct.id'),
$expr->eq('ct.version', Type::STATUS_DEFINED)
)
);

return $queryBuilder;
Expand Down Expand Up @@ -163,7 +177,8 @@ public function createVersionInfoFindQueryBuilder(): DoctrineQueryBuilder
'c.status AS ezcontentobject_status',
'c.name AS ezcontentobject_name',
'c.language_mask AS ezcontentobject_language_mask',
'c.is_hidden AS ezcontentobject_is_hidden'
'c.is_hidden AS ezcontentobject_is_hidden',
'ct.identifier AS content_type_identifier'
)
->from(Gateway::CONTENT_VERSION_TABLE, 'v')
->innerJoin(
Expand All @@ -180,6 +195,15 @@ public function createVersionInfoFindQueryBuilder(): DoctrineQueryBuilder
$expr->eq('t.contentobject_id', 'v.contentobject_id'),
$expr->eq('t.main_node_id', 't.node_id')
)
)
->innerJoin(
'c',
'ezcontentclass',
'ct',
$expr->and(
$expr->eq('c.contentclass_id', 'ct.id'),
$expr->eq('ct.version', Type::STATUS_DEFINED)
)
);

return $query;
Expand Down
8 changes: 6 additions & 2 deletions src/lib/Persistence/Legacy/Content/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,16 @@ private function loadCachedVersionFieldDefinitionsPerLanguage(
*
* @return \Ibexa\Contracts\Core\Persistence\Content\ContentInfo
*/
public function extractContentInfoFromRow(array $row, $prefix = '', $treePrefix = 'ezcontentobject_tree_')
{
public function extractContentInfoFromRow(
array $row,
$prefix = '',
$treePrefix = 'ezcontentobject_tree_'
) {
$contentInfo = new ContentInfo();
$contentInfo->id = (int)$row["{$prefix}id"];
$contentInfo->name = (string)$row["{$prefix}name"];
$contentInfo->contentTypeId = (int)$row["{$prefix}contentclass_id"];
$contentInfo->contentTypeIdentifier = (string)$row['content_type_identifier'];
$contentInfo->sectionId = (int)$row["{$prefix}section_id"];
$contentInfo->currentVersionNo = (int)$row["{$prefix}current_version"];
$contentInfo->ownerId = (int)$row["{$prefix}owner_id"];
Expand Down
Loading
Loading