Table of Contents |
---|
Assigning
...
Section to content
Info | ||
---|---|---|
| ||
https://github.com/ezsystems/CookbookBundle/tree/master/Command/AssignContentToSectionCommand.php |
The Section that a Content item belongs to can be set during creation, using the ContentCreateStruct::$sectionId
property. However, as for many Repository objects properties, the section Section can't be changed using a ContentUpdateStruct
. The reason is still the same: changing a Content item's section Section will affect the subtrees referenced by its Locations. For this reason, it is required that you use the SectionService to change the Section of a Content item.
Code Block | ||||
---|---|---|---|---|
| ||||
$contentInfo = $contentService->loadContentInfo( $contentId ); $section = $sectionService->loadSection( $sectionId ); $sectionService->assignSection( $contentInfo, $section ); |
...
Code Block | ||||
---|---|---|---|---|
| ||||
$section = $sectionService->loadSection( $sectionId ); |
SectionService::loadSection()
is then used to load the Section we want to assign our Content to. Note that there is no SectionInfo
object. Sections are quite simple, and we don't need to separate their metadata from their actual data. However, SectionCreateStruct
and SectionUpdateStruct
objects must still be used to create and update sectionsSections.
Code Block | ||||
---|---|---|---|---|
| ||||
$sectionService->assignSection( $contentInfo, $section ); |
The actual update operation is done using SectionService::assignSection()
, with the ContentInfo
and the Section as arguments.
Note |
---|
|
Creating a
...
Content Type
Info | ||
---|---|---|
| ||
https://github.com/ezsystems/CookbookBundle/blob/master/Command/CreateContentTypeCommand.php |
...
Using the create struct's properties, we can set the typeType's properties:
- the main language (
mainLanguageCode
) for the type Type is set to eng-GB, - the content name generation pattern (
nameSchema
) is set to '<title>': content Content items of this type will be named the same as their 'title' field. - the human-readable name for our type Type is set using the
names
property. We give it a hash, indexed by the locale ('eng-GB') the name is set in. This locale must exist in the system. - the same way that we have set the
names
property, we can set human-readable descriptions, again as hashes indexed by locale code.
The next big part is to add FieldDefinition objects to our ContentTypeContent Type.
Code Block | ||
---|---|---|
| ||
// add a TextLine Field with identifier 'title' $titleFieldCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct( 'title', 'ezstring' ); $titleFieldCreateStruct->names = array( 'eng-GB' => 'Title' ); $titleFieldCreateStruct->descriptions = array( 'eng-GB' => 'The Title' ); $titleFieldCreateStruct->fieldGroup = 'content'; $titleFieldCreateStruct->position = 10; $titleFieldCreateStruct->isTranslatable = true; $titleFieldCreateStruct->isRequired = true; $titleFieldCreateStruct->isSearchable = true; $contentTypeCreateStruct->addFieldDefinition( $titleFieldCreateStruct ); // add a TextLine Field body field $bodyFieldCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct( 'body', 'ezstring' ); $bodyFieldCreateStruct->names = array( 'eng-GB' => 'Body' ); $bodyFieldCreateStruct->descriptions = array( 'eng-GB' => 'Description for Body' ); $bodyFieldCreateStruct->fieldGroup = 'content'; $bodyFieldCreateStruct->position = 20; $bodyFieldCreateStruct->isTranslatable = true; $bodyFieldCreateStruct->isRequired = true; $bodyFieldCreateStruct->isSearchable = true; $contentTypeCreateStruct->addFieldDefinition( $bodyFieldCreateStruct ); |
We need to create a FieldDefinitionCreateStruct
object for each FieldDefinition
our ContentType
will be made of. Those objects are obtained using ContentTypeService::newFieldDefinitionCreateStruct()
. This method expects the FieldDefinition identifier and its type as arguments. The identifiers match the ones from eZ Publish 4 (ezstring
for TextLine, etc.).
Each field's properties are set using the create struct's properties:
names
anddescriptions
are set using hashes indexed by the locale code, and with the name or description as an argument.- The
fieldGroup
is set to 'content' - Fields are ordered using the
position
property, ordered numerically in ascending order. We set it to an integer. - The translatable, required ans and searchable boolean flags are set using their respective property:
isTranslatable
,isRequired
andisSearchable
.
Once the properties for each create struct are set, the field is added to the ContentType Content Type create struct using ContentTypeCreateStruct::addFieldDefinition()
.
...
The last step is the same as for Content: we create a content type Content Type draft using ContentTypeService::createContentType()
, with the ContentTypeCreateStruct
and an array of ContentTypeGroup
objects are arguments. We then publish the ContentType Content Type draft using ContentTypeService::publishContentTypeDraft()
.