remove an Image programmatically in ezpublish

413 views Asked by At

I have an Object in ezPublish which has an image attribute. this attribute has an image as value that I want to remove it programmatically in PHP. ( the image value and not attribute itself )

any idea how to do it ?

2

There are 2 answers

0
Heath On BEST ANSWER

For versions of eZ Publish (Legacy) prior to Oct 15, 2013, or git tag, 'v2014.03.1' including eZ Publish 4.7 the 'deleteStoredObjectAttribute' method -requires- a non-null second argument value be passed in versions of eZ Publish Legacy before the following commit:

See: https://github.com/ezsystems/ezpublish-legacy/commit/61aaa002c00ccfb86b3e02856b319f54b3405ef8

This includes eZ Publish 4.7 and thus the question author's specific use case. Which is why this answer is more accurate than all others.

Without this second non-null parameter, the aliases image files would be deleted from the filesystem but ... the image aliases information (alias references, meta data, etc) would still exist with the content object attribute content (database storage xml).

Without the second non-parameter the image would appear as still partially existing in the content object but the image preview, (IE: usage of the image path, meta data by the system) would display a broken image and thus then represent a corrupt / incomplete copy of the original content object attribute content.

For maximum Backwards Compatibility, it's best to always pass a non-null second parameter since versions of eZ Publish Legacy beyond 10/15/2013 do not even use the second parameter in any way.

The following is a complete example of the source code required to remove a content object image attribute content (and remove the related meta-data and image file from disk) the best way possible for nearly any version of eZ Publish Legacy.

// The following two variables are filled with dummy values.
// You will need to change the contents of these variables to match
// your actual use case identifiers (Content Object ID / Class Attribute Identifier)
$objectID = 42;
$objectImageAttributeIdentifier = 'profile_image';

$object = eZContentObject::fetch( $objectID );

$objectID = $object->attribute( 'id' );
$objectCurrentVersion = $object->attribute( 'current_version' );
$objectDataMap = $object->attribute( 'data_map' );

if ( isset( $objectDataMap[ $objectImageAttributeIdentifier ] ) )
{
    $objectImageAttribute = $objectDataMap[ $objectImageAttributeIdentifier ];

    if ( $objectImageAttribute->attribute( 'has_content' ) )
    {
        $objectImageDataType = $objectImageAttribute->dataType();
        $objectImageDataType->deleteStoredObjectAttribute( $objectImageAttribute, $objectCurrentVersion );

        eZContentCacheManager::clearContentCacheIfNeeded( $objectID );
    }
}
2
Bertrand Dunogier On

According to eZImageType::customObjectAttributeHTTPAction(), you should use eZImageType::deleteStoredObjectAttribute().

The method's implementation shows how it is done internally. The method won't delete the attribute itself, just the external data (image, aliases, set the xml to empty).