mirror of
https://github.com/jbcr/core.git
synced 2026-04-24 00:58:09 +02:00
Merge pull request #640 from bolt/dx-improvements
Various DX improvements: ContentRepository::findOneByFieldValue, Content::setDefinition, Content::setFieldValue
This commit is contained in:
@@ -168,4 +168,9 @@ class Config
|
||||
{
|
||||
return new Collection($this->get('general/accept_media_types'));
|
||||
}
|
||||
|
||||
public function getContentType(string $name): ?Collection
|
||||
{
|
||||
return $this->get('contenttypes/' . $name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,6 +119,12 @@ class ContentTypesParser extends BaseParser
|
||||
if (! isset($contentType['singleton'])) {
|
||||
$contentType['singleton'] = false;
|
||||
}
|
||||
if (! isset($contentType['record_template'])) {
|
||||
$contentType['record_template'] = $contentType['singular_slug'] . '.twig';
|
||||
}
|
||||
if (! isset($contentType['listing_template'])) {
|
||||
$contentType['listing_template'] = $contentType['slug'] . '.twig';
|
||||
}
|
||||
|
||||
if ($contentType['singleton']) {
|
||||
$contentType['listing_records'] = 1;
|
||||
|
||||
@@ -103,7 +103,6 @@ class ContentFixtures extends BaseFixture implements DependentFixtureInterface,
|
||||
$sortorder = 1;
|
||||
foreach ($contentType['fields'] as $name => $fieldType) {
|
||||
$field = Field::factory($fieldType, $name);
|
||||
$field->setName($name);
|
||||
|
||||
if (isset($preset[$name])) {
|
||||
$field->setValue($preset[$name]);
|
||||
|
||||
+31
-1
@@ -133,11 +133,16 @@ class Content
|
||||
*/
|
||||
private $taxonomies;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(?ContentType $contentTypeDefinition = null)
|
||||
{
|
||||
$this->createdAt = new \DateTime();
|
||||
$this->taxonomies = new ArrayCollection();
|
||||
$this->fields = new ArrayCollection();
|
||||
|
||||
if ($contentTypeDefinition) {
|
||||
$this->setContentType($contentTypeDefinition->getSlug());
|
||||
$this->setDefinition($contentTypeDefinition);
|
||||
}
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
@@ -167,6 +172,11 @@ class Content
|
||||
$this->contentTypeDefinition = ContentType::factory($this->contentType, $contentTypesConfig);
|
||||
}
|
||||
|
||||
public function setDefinition(ContentType $contentType): void
|
||||
{
|
||||
$this->contentTypeDefinition = $contentType;
|
||||
}
|
||||
|
||||
public function getDefinition(): ?ContentType
|
||||
{
|
||||
return $this->contentTypeDefinition;
|
||||
@@ -348,6 +358,17 @@ class Content
|
||||
return $this->getField($fieldName)->getParsedValue();
|
||||
}
|
||||
|
||||
public function setFieldValue(string $fieldName, $value): void
|
||||
{
|
||||
if (! $this->hasField($fieldName)) {
|
||||
$this->addFieldByName($fieldName);
|
||||
}
|
||||
|
||||
$field = $this->getField($fieldName);
|
||||
|
||||
$field->setValue($value);
|
||||
}
|
||||
|
||||
public function getField(string $fieldName): Field
|
||||
{
|
||||
if ($this->hasField($fieldName) === false) {
|
||||
@@ -393,6 +414,15 @@ class Content
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addFieldByName(string $fieldName): void
|
||||
{
|
||||
$definition = $this->contentTypeDefinition->get('fields')->get($fieldName);
|
||||
|
||||
$field = Field::factory($definition, $fieldName);
|
||||
|
||||
$this->addField($field);
|
||||
}
|
||||
|
||||
public function removeField(Field $field): self
|
||||
{
|
||||
unset($this->fields[$field->getName()]);
|
||||
|
||||
@@ -86,7 +86,13 @@ class Field implements Translatable, FieldInterface
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return implode(', ', $this->getValue());
|
||||
$value = '';
|
||||
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->getValue()));
|
||||
foreach ($iterator as $iteratorValue) {
|
||||
$value .= $iteratorValue .', ';
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public static function factory(LaravelCollection $definition, string $name = ''): self
|
||||
|
||||
@@ -299,13 +299,19 @@ final class BackendMenuBuilder implements BackendMenuBuilderInterface
|
||||
$result = [];
|
||||
|
||||
foreach ($records as $record) {
|
||||
$result[] = [
|
||||
'id' => $record->getId(),
|
||||
'name' => $this->contentExtension->getTitle($record),
|
||||
'link' => $this->contentExtension->getLink($record),
|
||||
'editLink' => $this->contentExtension->getEditLink($record),
|
||||
'icon' => $record->getIcon(),
|
||||
];
|
||||
try {
|
||||
$additionalResult = [
|
||||
'id' => $record->getId(),
|
||||
'name' => $this->contentExtension->getTitle($record),
|
||||
'link' => $this->contentExtension->getLink($record),
|
||||
'editLink' => $this->contentExtension->getEditLink($record),
|
||||
'icon' => $record->getIcon(),
|
||||
];
|
||||
|
||||
$result[] = $additionalResult;
|
||||
} catch (\RuntimeException $exception) {
|
||||
// When a record is not initialised (yet), don't break, but fail gracefully.
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
@@ -192,6 +192,18 @@ class ContentRepository extends ServiceEntityRepository
|
||||
)
|
||||
->andWhere('slug.value = :slug')
|
||||
->setParameter('slug', Json::json_encode([$slug]))
|
||||
->setMaxResults(1)
|
||||
->getQuery()
|
||||
->getOneOrNullResult();
|
||||
}
|
||||
|
||||
public function findOneByFieldValue(string $fieldName, $value): ?Content
|
||||
{
|
||||
return $this->getQueryBuilder()
|
||||
->innerJoin('content.fields', 'field')
|
||||
->andWhere('field.value = :value')
|
||||
->setParameter('value', Json::json_encode([$value]))
|
||||
->setMaxResults(1)
|
||||
->getQuery()
|
||||
->getOneOrNullResult();
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class ContentTypesParserTest extends ParserTestBase
|
||||
{
|
||||
public const NUMBER_OF_CONTENT_TYPES_IN_MINIMAL_FILE = 2;
|
||||
|
||||
public const AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE = 20;
|
||||
public const AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE = 22;
|
||||
|
||||
public function testCanParse(): void
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user