Skip to content

Instantly share code, notes, and snippets.

@arfaram
Last active October 26, 2021 17:00
Show Gist options
  • Save arfaram/e21c1950fe6e1a46d70e7873b103ba72 to your computer and use it in GitHub Desktop.
Save arfaram/e21c1950fe6e1a46d70e7873b103ba72 to your computer and use it in GitHub Desktop.
This is an example on how you can create eZPlatform ContentType with all existing FieldTypes using the KaliopMigrationBundle. The second example is using the Public API to create the content. This example demonstarte how to set the different eZPlatform FieldTypes
-
type: content_type
mode: create
content_type_group: Content
identifier: ez_content_fieldtypes
name:
eng-GB: 'eZ Content FieldType'
description: { }
name_pattern: '<title>'
url_name_pattern: ''
is_container: false
lang: eng-GB
attributes:
-
identifier: title
type: ezstring
name:
eng-GB: Title
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 1
field-settings: { }
validator-configuration:
StringLengthValidator: { maxStringLength: null, minStringLength: null }
-
identifier: description
type: ezrichtext
name:
eng-GB: Description
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 2
field-settings: { }
validator-configuration: { }
-
identifier: author
type: ezauthor
name:
eng-GB: Author
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 3
field-settings: { }
validator-configuration: { }
-
identifier: image
type: ezimage
name:
eng-GB: Image
description: { }
required: false
searchable: false
info-collector: false
disable-translation: false
category: content
position: 4
field-settings: { }
validator-configuration:
FileSizeValidator: { maxFileSize: null }
-
identifier: version
type: ezinteger
name:
eng-GB: Version
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 5
field-settings: { }
validator-configuration:
IntegerValueValidator: { minIntegerValue: null, maxIntegerValue: null }
-
identifier: conference_location
type: ezselection
name:
eng-GB: 'Conference Location'
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 6
field-settings:
isMultiple: true
options: [London, Cologne, 'New York']
validator-configuration: { }
-
identifier: city_map
type: ezgmaplocation
name:
eng-GB: 'City Map'
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 7
field-settings: { }
validator-configuration: { }
-
identifier: fileupload
type: ezbinaryfile
name:
eng-GB: 'File Upload'
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 8
field-settings: { }
validator-configuration:
FileSizeValidator: { maxFileSize: null }
-
identifier: available_places
type: ezboolean
name:
eng-GB: 'Available Places'
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 9
field-settings: { }
validator-configuration: { }
-
identifier: country
type: ezcountry
name:
eng-GB: Country
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 10
field-settings:
isMultiple: true
validator-configuration: { }
-
identifier: datetime
type: ezdatetime
name:
eng-GB: 'Conference Datetime'
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 11
field-settings:
defaultType: 0
useSeconds: false
dateInterval: null
validator-configuration: { }
-
identifier: date
type: ezdate
name:
eng-GB: Date
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 12
field-settings:
defaultType: 0
validator-configuration: { }
-
identifier: email
type: ezemail
name:
eng-GB: E-Mail
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 13
field-settings: { }
validator-configuration:
EmailAddressValidator: { }
-
identifier: fee
type: ezfloat
name:
eng-GB: 'Registration Fee'
description: { }
required: false
searchable: false
info-collector: false
disable-translation: false
category: content
position: 14
field-settings: { }
validator-configuration:
FloatValueValidator: { minFloatValue: null, maxFloatValue: null }
-
identifier: isbn
type: ezisbn
name:
eng-GB: ISBN
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 15
field-settings:
isISBN13: true
validator-configuration: { }
-
identifier: keywords
type: ezkeyword
name:
eng-GB: Keywords
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 16
field-settings: { }
validator-configuration: { }
-
identifier: media_file
type: ezmedia
name:
eng-GB: 'Media File'
description: { }
required: false
searchable: false
info-collector: false
disable-translation: false
category: content
position: 17
field-settings:
mediaType: html5_video
validator-configuration:
FileSizeValidator: { maxFileSize: null }
-
identifier: rating
type: ezsrrating
name:
eng-GB: Rating
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 18
field-settings: { }
validator-configuration: { }
-
identifier: relation_single
type: ezobjectrelation
name:
eng-GB: 'Relation single'
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 19
field-settings:
selectionMethod: 0
selectionRoot: null
selectionContentTypes: { }
validator-configuration: { }
-
identifier: relation_multiple
type: ezobjectrelationlist
name:
eng-GB: 'Relation Multiple'
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 20
field-settings:
selectionMethod: 0
selectionDefaultLocation: null
selectionContentTypes: { }
validator-configuration: { }
-
identifier: text
type: eztext
name:
eng-GB: Text
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 21
field-settings:
textRows: 10
validator-configuration: { }
-
identifier: time
type: eztime
name:
eng-GB: Time
description: { }
required: false
searchable: true
info-collector: false
disable-translation: false
category: content
position: 22
field-settings:
defaultType: 0
useSeconds: false
validator-configuration: { }
-
identifier: url
type: ezurl
name:
eng-GB: URL
description: { }
required: false
searchable: false
info-collector: false
disable-translation: false
category: content
position: 23
field-settings: { }
validator-configuration: { }
-
identifier: user_account
type: ezuser
name:
eng-GB: 'User Account'
description: { }
required: false
searchable: false
info-collector: false
disable-translation: false
category: content
position: 24
field-settings: { }
validator-configuration: { }
<?php
/**
* Usage: php app/console ezpublish:ez_content_fieldtypes PARENT-LOCATION-ID
*/
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use eZ\Publish\Core\FieldType\Author\Author;
use eZ\Publish\Core\FieldType\Image\Value as Image;
use eZ\Publish\Core\FieldType\Selection\Value as SelectionValue;
use eZ\Publish\Core\FieldType\BinaryFile\Value as BinaryFileValue;
use eZ\Publish\Core\FieldType\DateAndTime\Value as DateAndTimeValue;
use eZ\Publish\Core\FieldType\Date\Value as DateValue;
use eZ\Publish\Core\FieldType\EmailAddress\Value as EmailAddressValue;
use eZ\Publish\Core\FieldType\Float\Value as FloatValue;
use eZ\Publish\Core\FieldType\ISBN\Value as ISBNValue;
use eZ\Publish\Core\FieldType\Media\Value as MediaValue;
use eZ\Publish\Core\FieldType\Url\Value as UrlValue;
//use eZ\Publish\Core\FieldType\User\Value as UserValue;
class CreateEzContentFieldtypesCommand extends ContainerAwareCommand
{
protected function configure()
{
//check use in the class description
$this->setName( 'ezpublish:ez_content_fieldtypes' )->setDefinition(
array(
new InputArgument( 'parentLocationId', InputArgument::REQUIRED, 'An existing parent location ID' )
)
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// fetch the input arguments
$parentLocationId = $input->getArgument( 'parentLocationId' );
try{
/** @var $repository \eZ\Publish\API\Repository\Repository */
$repository = $this->getContainer()->get( 'ezpublish.api.repository' );
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$contentTypeService = $repository->getContentTypeService();
//@deprecated since 6.6, to be removed. See 'getPermissionResolver' below
//$repository->setCurrentUser($repository->getUserService()->loadUserByLogin( 'admin' ));
//Do not put ID in php file use instead e.g parameters.yml.
$repository->getPermissionResolver()->setCurrentUserReference( $repository->getUserService()->loadUser( $this->getContainer()->getParameter('RootUserId') ) );
//$repository->getPermissionResolver()->setCurrentUserReference( $repository->getUserService()->loadUser( 14 ) );
$contentType = $contentTypeService->loadContentTypeByIdentifier('ez_content_fieldtypes');
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
//Author
$contentCreateStruct->setField('author',
array(
new Author(array('name' => 'John Doe', 'email' => 'me@ez.no')),
new Author(array('name' => 'Jane Doe', 'email' => 'me@ez.no')),
)
);
//BinaryFile
$file = '/home/ez/Documents/pdffile.pdf';
$contentCreateStruct->setField('fileupload',
new BinaryFileValue(
array(
'inputUri' => $file,
'fileName' => basename($file),
'fileSize' => filesize($file),
'downloadCount' => 0,
'mimeType' => null,
)
)
);
//CheckBox
$contentCreateStruct->setField('available_places',true);
//Country
//Check Alpha2 List in eZ/Publish/Core/settings/fieldtypes.yml
$contentCreateStruct->setField('country',
array('DE', 'GB')
);
//DateAndTime
$contentCreateStruct->setField('datetime',
DateAndTimeValue::fromString('2018-06-05 09:00 Europe/Berlin')
);
//Date
$contentCreateStruct->setField('date',
DateValue::fromTimestamp($timestamp = 1528182000)
);
//email address
$contentCreateStruct->setField('email',
new EmailAddressValue('contact@ez.no')
);
//Float
$contentCreateStruct->setField('fee',
new FloatValue(450.50)
);
//Image
$image = '/home/ez/Pictures/ezsystems.png';
$value = new Image(
array(
'path' => $image,
'fileSize' => filesize( $image ),
'fileName' => basename( $image ),
'alternativeText' => 'Ez Systems'
)
);
$contentCreateStruct->setField( 'image', $value );
//Integer
$contentCreateStruct->setField('version', 2);
//ISBN
$contentCreateStruct->setField('isbn',
new ISBNValue('978-972-25-1409-5')
);
//Keywords
$contentCreateStruct->setField('keywords', array('ez conference 2018','ez workshops','ez hackathon'));
//Map Location
$contentCreateStruct->setField('city_map',array(
'address' => 'Cologne',
'latitude' => (float) '50.943821',
'longitude' => (float) '6.942233'
)
);
//Media
$media_file = '/home/ez/Videos/eZ_Conference_2017_London.mp4';
$contentCreateStruct->setField('media_file',
new MediaValue(
array(
'inputUri' => $media_file,
'fileName' => basename($media_file),
'fileSize' => filesize($media_file),
'hasController' => true,
'mimeType' => mime_content_type($media_file),
'autoplay' => true,
'loop' => false,
'width' => 500,
'height' => 300,
'uri' => '',
)
)
);
//Rating
//No default frontend template is available
$contentCreateStruct->setField('rating', true);
//Relation (Single)
$contentCreateStruct->setField('relation_single', 54);
//RelationList (Multiple)
$contentCreateStruct->setField('relation_multiple', array(54, 98));
//RichText
$descriptionXML = '<?xml version="1.0" encoding="UTF-8"?><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><emphasis>We provide developers,</emphasis> editors and marketers with an open content management system for creating customer experiences. <emphasis role="underlined">With our technology</emphasis> you have the tools to build long-lasting relationships with your users throughout the customer journey - no matter the <emphasis role="strong">platform</emphasis>, channel or device.</para></section>';
$contentCreateStruct->setField('description', $descriptionXML);
//Selection
$contentCreateStruct->setField('conference_location',
new SelectionValue(array(1, 2))
);
//TextBlock
$contentCreateStruct->setField('text', '<p>Where Content Means Business<h3>eZ Platform</h3></p>');
//TextLine
$contentCreateStruct->setField('title', 'Where Content Means Business');
//Time
$contentCreateStruct->setField('time', '09:00:00 am');
//URL
$contentCreateStruct->setField('url',
new UrlValue(
'http://conference.ez.no',
'Save the Date: eZ Conference 2018 - Cologne, June 5-7!')
);
//User
// will not work. please see below example:
/*
$contentCreateStruct->setField('user',
new UserValue(
array(
'hasStoredLogin' => false,
'login' => 'sindelfingen',
'email' => 'me@ez.no',
'passwordHash' => '4e6f6184135228ccd45f8233d72a0363',
'passwordHashType' => '2',
'enabled' => true,
'maxLogin' => 1000,
)
)
);
*/
//Use instead the UserService
// Working example:
/*
$userService = $repository->getUserService();
$userCreateStruct = $userService->newUserCreateStruct(
'jandoe',
'me@ez.no',
'publish',
'ger-DE',
$contentType
);
$userGroup = $userService->loadUserGroup(12); //Adminstrator UserGroup
$content = $userService->createUser($userCreateStruct,array($userGroup));
$output->writeln("User created with ID:". $content->getFieldValue('user_account')->contentId);
*/
// instantiate a location create struct from the parent location
$locationCreateStruct = $locationService->newLocationCreateStruct($parentLocationId);
// create a draft using the content and location create struct and publish it
$draft = $contentService->createContent($contentCreateStruct, array($locationCreateStruct));
$content = $contentService->publishVersion($draft->versionInfo);
$output->writeln("<info>Content created with ID $content->id");
}// Content type or location not found
catch (\eZ\Publish\API\Repository\Exceptions\NotFoundException $e) {
$output->writeln($e->getMessage());
} // Invalid field value
catch (\eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $e) {
$output->writeln($e->getMessage());
} // Required field missing or empty
catch (\eZ\Publish\API\Repository\Exceptions\ContentValidationException $e) {
$output->writeln($e->getMessage());
} // Unauthorized User
catch (\eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e) {
$output->writeln($e->getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment