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

Image asset complex field handler #203

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Core/FieldHandler/EzImageAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Kaliop\eZMigrationBundle\Core\FieldHandler;

use eZ\Publish\Core\FieldType\ImageAsset\Value;
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
use Kaliop\eZMigrationBundle\API\FieldDefinitionConverterInterface;
use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher;

class EzImageAsset extends AbstractFieldHandler implements FieldValueImporterInterface
{
protected $contentMatcher;

public function __construct(ContentMatcher $contentMatcher)
{
$this->contentMatcher = $contentMatcher;
}

/**
* Creates a value object to use as the field value when setting an ez image asset field type.
*
* @param array|string|int $fieldValue The definition of the field value, structured in the yml file
* @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
* @return Value
*/
public function hashToFieldValue($fieldValue, array $context = array())
{
$altText = '';

if ($fieldValue === null) {
return new Value();
}
if (isset($fieldValue['alt_text'])) {
$altText = $fieldValue['alt_text'];
}

if (is_array($fieldValue) && array_key_exists('destinationContentId', $fieldValue)) {
// fromHash format
$id = $fieldValue['destinationContentId'];
} else {
// simplified format
$id = $fieldValue;
}

if ($id === null) {
return new Value();
}

// 1. resolve relations
$id = $this->referenceResolver->resolveReference($id);
// 2. resolve remote ids
$id = $this->contentMatcher->matchOneByKey($id)->id;

return new Value($id, $altText);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that the $id is int? I had similar issues when using references https://github.com/ezsystems/ezplatform-ee-demo/blob/v2.5.1/src/AppBundle/Migration/FieldHandler/EzImageAsset.php#L37

is your solution works with content exporter command?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @florianalexandre any answer?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any plans to merge this?

}
}
9 changes: 9 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ parameters:
ez_migration_bundle.complex_field.ezimage.class: Kaliop\eZMigrationBundle\Core\FieldHandler\EzImage
ez_migration_bundle.complex_field.ezmedia.class: Kaliop\eZMigrationBundle\Core\FieldHandler\EzMedia
ez_migration_bundle.complex_field.ezpage.class: Kaliop\eZMigrationBundle\Core\FieldHandler\EzPage
ez_migration_bundle.complex_field.ezimageasset.class: Kaliop\eZMigrationBundle\Core\FieldHandler\EzImageAsset
ez_migration_bundle.complex_field.ezrelation.class: Kaliop\eZMigrationBundle\Core\FieldHandler\EzRelation
ez_migration_bundle.complex_field.ezrelationlist.class: Kaliop\eZMigrationBundle\Core\FieldHandler\EzRelationList
ez_migration_bundle.complex_field.ezrichtext.class: Kaliop\eZMigrationBundle\Core\FieldHandler\EzRichText
Expand Down Expand Up @@ -661,6 +662,14 @@ services:
tags:
- { name: ez_migration_bundle.complex_field, fieldtype: ezobjectrelation, priority: 0 }

ez_migration_bundle.complex_field.ezimageasset:
parent: ez_migration_bundle.complex_field
class: '%ez_migration_bundle.complex_field.ezimageasset.class%'
arguments:
- '@ez_migration_bundle.content_matcher'
tags:
- { name: ez_migration_bundle.complex_field, fieldtype: ezimageasset, priority: 0 }

ez_migration_bundle.complex_field.ezrelationlist:
parent: ez_migration_bundle.complex_field
class: '%ez_migration_bundle.complex_field.ezrelationlist.class%'
Expand Down
40 changes: 40 additions & 0 deletions Tests/dsl/UnitTest0190_create_eZImage_content_type.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-
type: content_type
mode: update
remove_drafts: true
match:
contenttype_identifier: image
attributes:
-
identifier: slide_format
type: ezselection
name:
fre-FR: Format
description: { }
required: true
searchable: false
info-collector: false
disable-translation: false
category: content
position: 4
field-settings:
isMultiple: false
options: [Square, Landscape, Portrait]
validator-configuration: { }

-
type: content
mode: delete
match:
content_type_identifier: gallery_slide
-
type: trash
mode: delete
match:
content_type_identifier: gallery_slide
-
type: content_type
mode: delete
remove_drafts: true
match:
identifier: gallery_slide
27 changes: 27 additions & 0 deletions Tests/dsl/UnitTest0191_create_eZImage_content.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-
type: content
mode: create
content_type: image
parent_location: 2
priority: 0
is_hidden: false
sort_field: path
sort_order: ASC
remote_id: 0c6ca3b36823ab38b9c8a8b79d5b12fd
location_remote_id: f17baf9b7fac9fa91ea2c1f76dcbd39c
lang: fre-FR
section: 1
owner: 14
modification_date: 1556889738
publication_date: 1556889738
always_available: true
attributes:
name: 'eZPlatform img'
caption:
xml: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:ezxhtml=\"http://ez.no/xmlns/ezpublish/docbook/xhtml\" xmlns:ezcustom=\"http://ez.no/xmlns/ezpublish/docbook/custom\" version=\"5.0-variant ezpublish-1.0\"><para>eZPLatform</para></section>\n"
image:
path: images/blank.jpg
filename: blank.jpg
alternativeText: ''
slide_format:
- 0
44 changes: 44 additions & 0 deletions Tests/dsl/UnitTest0192_create_eZImageAsset_content_type.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-
type: content_type
mode: create
content_type_group: ez
identifier: kaliop_ezimageasset
name:
fre-FR: 'Kaliop'
description: { }
name_pattern: '<title>'
url_name_pattern: '<title>'
is_container: true
lang: fre-FR
attributes:
-
identifier: title
type: ezstring
name:
fre-FR: Title
description:
fre-FR: 'Title'
required: true
searchable: true
info-collector: false
disable-translation: false
category: content
position: 1
field-settings: { }
validator-configuration:
StringLengthValidator: { maxStringLength: 250, minStringLength: 1 }
-
identifier: image_vignette
type: ezimageasset
name:
fre-FR: 'Image'
description:
fre-FR: 'Image ezimageasset'
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 4
field-settings: { }
validator-configuration: { }
22 changes: 22 additions & 0 deletions Tests/dsl/UnitTest0193_create_eZImageAsset_content.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-
type: content
mode: create
content_type: kaliop_ezimageasset
parent_location: 2 #HP HDA
priority: 0
is_hidden: false
sort_field: published
sort_order: DESC
remote_id: a50df951d9eeda40b76ca6a323a958c3
location_remote_id: b62b6f211fb326f77b9cfea6c6e57944
lang: fre-FR
section: 1
owner: 14
modification_date: 1556869563
publication_date: 1556813177
always_available: true
attributes:
title: 'ez platform'
image_vignette:
destinationContentId: 0c6ca3b36823ab38b9c8a8b79d5b12fd
alternativeText: null
21 changes: 21 additions & 0 deletions Tests/dsl/UnitTest0194_delete_all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-
type: content
mode: delete
match:
location_remote_id: f17baf9b7fac9fa91ea2c1f76dcbd39c
-
type: content
mode: delete
match:
location_remote_id: b62b6f211fb326f77b9cfea6c6e57944
-
type: content_type
mode: delete
match:
identifier: image

-
type: content_type
mode: delete
match:
identifier: kaliop_ezimageasset
Binary file added Tests/dsl/images/blank.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.