entity_share_field_collection.import.inc in Entity Share 7
Class for handling Field Collection Import.
File
modules/entity_share_field_collection/includes/entity_share_field_collection.import.incView source
<?php
/**
* @file
* Class for handling Field Collection Import.
*/
/**
* A class to import the field collection.
*
* @property EntityShareEntityImport entityShareEntity
*/
class EntityShareFieldCollectionImport extends EntityShareFieldCollectionAbstract {
const WATCHDOG_TYPE = 'entity_share_fc_import';
/**
* Manage field collection for the import.
*/
public function importDatas() {
// Check if the field type is managed by the module.
if (!$this
->isManagedFieldType()) {
return;
}
// Keep some needed data that will be overrided
// by this code to inject them after.
if (isset($this->fieldData['entity_share'])) {
$bck_entity_share = $this->fieldData['entity_share'];
}
// Security to not have invalid field collection entities.
if (!$this
->isValidFieldCollectionEntity()) {
return;
}
// Get the updated fc entity.
$fc_entity = $this
->getUpdatedFieldCollection($this->fieldEntityType);
// Set the entity share property from backup.
if (isset($bck_entity_share)) {
$fc_entity->entity_share = $bck_entity_share;
}
// Attach the field collection to the local entity.
$this->entity->{$this->fieldName}[$this->fieldLang][$this->fieldDelta] = array(
'entity' => $fc_entity,
);
// Recursion on field import of the field collection
// to import file etc.
$this->entityShareEntity
->contentFieldWalk($fc_entity);
}
/**
* Check if valid field collection entity.
*
* @return bool
* TRUE if the field collection is valid, FALSE otherwise.
*/
protected function isValidFieldCollectionEntity() {
$entity_info = entity_get_info($this->fieldEntityType);
$uuid_key = $entity_info['entity keys']['uuid'];
/* @var $fc_entity FieldCollectionItemEntity */
// Uuid.
$uuid = $this->fieldData[$uuid_key];
// Security to not have invalid field collection entities.
return empty($uuid) ? FALSE : TRUE;
}
/**
* Get the created or updated field collection entity.
*
* @param string $field_entity_type
* Entity type of the field.
*
* @return object
* The field collection entity.
*
* @throws EntityShareExportException
* Throw exception if there is a problem
* with the field collection to import.
*/
protected function getUpdatedFieldCollection($field_entity_type) {
$entity_id_info = $this->entityShareEntity
->getLocalEntityIds((object) $this->fieldData);
// Existing field collection.
if (isset($entity_id_info['entity_id'])) {
// Update.
// Load current entity.
$fc_entity = entity_load($field_entity_type, array(
$entity_id_info['entity_id'],
));
if (empty($fc_entity)) {
watchdog(self::WATCHDOG_TYPE, 'The field collection id %fcid can\'t be loaded', array(
'%fcid' => $entity_id_info['entity_id'],
), WATCHDOG_ERROR);
throw new EntityShareExportException('The field collection id ' . $entity_id_info['entity_id'] . ' can\'t be loaded');
}
$fc_entity = $fc_entity[$entity_id_info['entity_id']];
// Populate entity fields.
foreach ($fc_entity as $prop => $prop_value) {
// All field machine name always start with "field_".
if (substr($prop, 0, 6) == 'field_') {
$fc_entity->{$prop} = $this->fieldData[$prop];
}
}
}
else {
// Create.
$fc_entity = entity_create($field_entity_type, $this->fieldData);
// FALSE cos we create the link manually with the host entity
// to not have unneeded empty field collection.
$fc_entity
->setHostEntity($this->entityShareEntity
->getEntityType(), $this->entityShareEntity
->getEntity(), $this->fieldLang, FALSE);
}
return $fc_entity;
}
}
Classes
Name | Description |
---|---|
EntityShareFieldCollectionImport | A class to import the field collection. |