Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,059 of 9,059 pages
About the help in other languages
All help pages

Creating and updating content in code

Create, update, move and remove content from PHP - eZContentFunctions::createAndPublishObject and updateAndPublishObject, fromString values, rich text, images, remote ids for idempotent imports, workflows and…

Download as PDF

Address: https://doc.exponential.earth/exphelp/viewer/help/creators/app-creators/creating-content-in-code.md

On this page

Creating and updating content in code

This page is for application creators who write content from code: imports, synchronizations, forms that create objects, migrations. The kernel's eZContentFunctions (kernel/classes/ezcontentfunctions.php) creates and publishes in one call, with the same operation (and triggers) as the editor.

Contents

  1. Creating and publishing
  2. Updating
  3. Values for each datatype
  4. Idempotent imports with remote IDs
  5. Moving, hiding, removing
  6. Large operations

Creating and publishing

$object = eZContentFunctions::createAndPublishObject( array(
    'creator_id'       => $userID,
    'class_identifier' => 'article',
    'parent_node_id'   => $parentNodeID,
    'remote_id'        => 'feed-item-' . $externalID,
    'attributes'       => array(
        'title' => 'A new article',
        'intro' => $introXml,
        'body'  => $bodyXml ) ) );

if ( !$object instanceof eZContentObject )
    throw new RuntimeException( 'Could not create the article' );

Attribute values are given in their fromString() form. Publishing runs the content/publish operation, so workflows on its triggers run too, and caches and the search index are updated.

Updating

eZContentFunctions::updateAndPublishObject( $object, array(
    'attributes' => array( 'title' => 'A better title' ) ) );

A new version is created and published; the previous one becomes archived, like an editor's edit.

Values for each datatype

DatatypeString form
ezstring, eztextthe text
ezinteger, ezfloat, ezboolean42, 3.5, 1
ezdate, ezdatetimea Unix timestamp
ezxmltextthe internal XML of rich text (the <section> document)
ezobjectrelationlistobject IDs separated by -
ezselectionthe selected option IDs
ezimage, ezbinaryfilea file path (and for images an alternative text after a separator)

The exact format of each is in its datatype class's fromString() in kernel/classes/datatypes/<name>/; read it before importing a datatype for the first time.

Idempotent imports with remote IDs

Give imported objects a remote ID built from the source's ID. On the next run, update instead of create:

$remoteID = 'feed-item-' . $externalID;
$existing = eZContentObject::fetchByRemoteID( $remoteID );
if ( $existing )
    eZContentFunctions::updateAndPublishObject( $existing, array( 'attributes' => $attributes ) );
else
    eZContentFunctions::createAndPublishObject( array( 'remote_id' => $remoteID ) + $params );

Running an import twice must not create duplicates.

Moving, hiding, removing

Use the kernel operations rather than SQL: eZContentObjectTreeNodeOperations::move(), eZContentOperationCollection for hide and reveal, eZContentObjectTreeNode::removeSubtrees() for removal (to the trash or not). They keep URL aliases, caches and the search index right.

Large operations

Thousands of objects belong in a background process: a command run from cron, a queue job (Queues with expqueue), or the kernel's content jobs for removes, copies and moves of large subtrees (Content jobs). Commit in batches, clear eZContentObject::clearCache() between batches, and log progress.

Become an Exponential Partner

Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x