eZ Community » Blogs » Thiago Campos Viana » Tip: Creating content classes using PHP

By

Tip: Creating content classes using PHP

Thursday 08 September 2011 4:11:38 am

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

In this post I will show a quick code snippet that creates a simple content class using php.

 

$user = eZUser::currentUser();
$userID = $user->attribute( 'contentobject_id' );
 
$class = eZContentClass::create( $userID );
 
$class->setAttribute( 'version', 0);
$class->setAttribute( 'name', 'Issue' );
$class->setAttribute( 'identifier', 'issue' );
$class->setAttribute( 'is_container', 1 );
$class->setAttribute( 'contentobject_name', '<title>' );
 
$class->store();
 
$ClassID = $class->attribute( 'id' );
 
 
$ClassVersion = $class->attribute( 'version' );
$ingroup = eZContentClassClassGroup::create( $ClassID, $ClassVersion,1,"Content" );
$ingroup->store();
 
// Title 
$DataTypeString = 'ezstring';
$new_attribute = eZContentClassAttribute::create( $ClassID,$DataTypeString );
 
$new_attribute->setAttribute( 'version', $ClassVersion);
$new_attribute->setAttribute( 'name', 'Title' );
$new_attribute->setAttribute( 'is_required', 1 );
$new_attribute->setAttribute( 'is_searchable', 1 );
$new_attribute->setAttribute( 'can_translate', 1 );
$new_attribute->setAttribute( 'identifier','title' );
 
$dataType = $new_attribute->dataType();
$dataType->initializeClassAttribute( $new_attribute );
$new_attribute->store();
// summary
$DataTypeString = 'ezxmltext';
$new_attribute = eZContentClassAttribute::create( $ClassID,$DataTypeString );
 
$new_attribute->setAttribute( 'version', $ClassVersion);
$new_attribute->setAttribute( 'name', 'Summary' );
$new_attribute->setAttribute( 'is_required', 1 );
$new_attribute->setAttribute( 'is_searchable', 1 );
$new_attribute->setAttribute( 'can_translate', 1 );
$new_attribute->setAttribute( 'identifier','summary' );
 
$dataType = $new_attribute->dataType();
$dataType->initializeClassAttribute( $new_attribute );
$new_attribute->store();
// description 
$DataTypeString = 'ezxmltext';
$new_attribute = eZContentClassAttribute::create( $ClassID,$DataTypeString );
 
$new_attribute->setAttribute( 'version', $ClassVersion);
$new_attribute->setAttribute( 'name', 'Description' );
$new_attribute->setAttribute( 'is_required', 1 );
$new_attribute->setAttribute( 'is_searchable', 1 );
$new_attribute->setAttribute( 'can_translate', 1 );
$new_attribute->setAttribute( 'identifier','description' );
 
$dataType = $new_attribute->dataType();
$dataType->initializeClassAttribute( $new_attribute );
$new_attribute->store();
 
// Type
$DataTypeString = 'ezinteger';
$new_attribute = eZContentClassAttribute::create( $ClassID,$DataTypeString );
 
$new_attribute->setAttribute( 'version', $ClassVersion);
$new_attribute->setAttribute( 'name', 'Type' );
$new_attribute->setAttribute( 'is_required', 1 );
$new_attribute->setAttribute( 'is_searchable', 1 );
 
$new_attribute->setAttribute( 'identifier','type' );
 
$dataType = $new_attribute->dataType();
$dataType->initializeClassAttribute( $new_attribute );
$new_attribute->store();
 
// Status
$DataTypeString = 'ezinteger';
$new_attribute = eZContentClassAttribute::create( $ClassID,$DataTypeString );
 
$new_attribute->setAttribute( 'version', $ClassVersion);
$new_attribute->setAttribute( 'name', 'Status' );
$new_attribute->setAttribute( 'is_required', 1 );
$new_attribute->setAttribute( 'is_searchable', 1 );
 
$new_attribute->setAttribute( 'identifier','status' );
 
$dataType = $new_attribute->dataType();
$dataType->initializeClassAttribute( $new_attribute );
$new_attribute->store();
 
// Priority
$DataTypeString = 'ezinteger';
$new_attribute = eZContentClassAttribute::create( $ClassID,$DataTypeString );
 
$new_attribute->setAttribute( 'version', $ClassVersion);
$new_attribute->setAttribute( 'name', 'Priority' );
$new_attribute->setAttribute( 'is_required', 1 );
$new_attribute->setAttribute( 'is_searchable', 1 );
 
$new_attribute->setAttribute( 'identifier','priority' );
 
$dataType = $new_attribute->dataType();
$dataType->initializeClassAttribute( $new_attribute );
$new_attribute->store();
 
 
// Screen Shot 
$DataTypeString = 'ezimage';
$new_attribute = eZContentClassAttribute::create( $ClassID,$DataTypeString );
$new_attribute->setAttribute( 'version', $ClassVersion);
$new_attribute->setAttribute( 'name', 'Screen Shot' );
$new_attribute->setAttribute( 'identifier','image' );
$dataType = $new_attribute->dataType();
$dataType->initializeClassAttribute( $new_attribute );
$new_attribute->store();
Proudly Developed with from