View source
<?php
class FieldCollectionItemEntity extends Entity {
protected $fieldInfo;
protected $hostEntity;
protected $hostEntityId;
protected $hostEntityRevisionId;
protected $hostEntityType;
protected $langcode = LANGUAGE_NONE;
public $item_id;
public $revision_id;
public $field_name;
public $default_revision = TRUE;
public $archived = FALSE;
public function __construct(array $values = array(), $entityType = NULL) {
parent::__construct($values, 'field_collection_item');
if (isset($this->field_name)) {
$field_info = $this
->fieldInfo();
if (!$field_info || $field_info['type'] != 'field_collection') {
throw new Exception("Invalid field name given: {$this->field_name} is not a Field Collection field.");
}
}
}
public function fieldInfo() {
return field_info_field($this->field_name);
}
public function instanceInfo() {
if ($this
->fetchHostDetails()) {
return field_info_instance($this
->hostEntityType(), $this->field_name, $this
->hostEntityBundle());
}
}
public function translatedInstanceLabel($langcode = NULL) {
if ($info = $this
->instanceInfo()) {
if (module_exists('i18n_field')) {
return i18n_string("field:{$this->field_name}:{$info['bundle']}:label", $info['label'], array(
'langcode' => $langcode,
'sanitize' => FALSE,
));
}
return $info['label'];
}
}
public function defaultLabel() {
if ($this
->fetchHostDetails()) {
$field = $this
->fieldInfo();
$label = $this
->translatedInstanceLabel();
$host = $this
->hostEntity();
if ($new_label = module_invoke_all('field_collection_item_label', $this, $host, $field, $label)) {
return array_pop($new_label);
}
if ($field['cardinality'] == 1) {
return $label;
}
if ($this->item_id) {
return t('!instance_label @count', array(
'!instance_label' => $label,
'@count' => $this
->delta() + 1,
));
}
return t('New !instance_label', array(
'!instance_label' => $label,
));
}
return t('Unconnected field collection item');
}
public function path() {
if ($this->item_id) {
return field_collection_field_get_path($this
->fieldInfo()) . '/' . $this->item_id;
}
}
public function defaultUri() {
return array(
'path' => $this
->path(),
);
}
public function setHostEntity($entity_type, $entity, $langcode = LANGUAGE_NONE, $create_link = TRUE) {
if (!empty($this->is_new)) {
$this->hostEntityType = $entity_type;
$this->hostEntity = $entity;
$this->langcode = $langcode;
list($this->hostEntityId, $this->hostEntityRevisionId) = entity_extract_ids($this->hostEntityType, $this->hostEntity);
if (!isset($this->hostEntityId)) {
$this->hostEntityId = FALSE;
}
if (!entity_revision_is_default($entity_type, $entity)) {
$this->hostEntityId = FALSE;
$this->archived = TRUE;
}
if ($create_link) {
$entity->{$this->field_name}[$this->langcode][] = array(
'entity' => $this,
);
}
}
else {
throw new Exception('The host entity may be set only during creation of a field collection item.');
}
}
public function updateHostEntity($entity, $host_entity_type = NULL) {
$this
->fetchHostDetails($entity);
if (!$this->hostEntityType || isset($entity->tid)) {
$this->hostEntityType = $host_entity_type;
$this->hostEntity = $entity;
list($this->hostEntityId, $this->hostEntityRevisionId) = entity_extract_ids($this->hostEntityType, $this->hostEntity);
}
list($recieved_id) = entity_extract_ids($this->hostEntityType, $entity);
if (!empty($this->hostEntityId) && $this
->isInUse()) {
if (is_array($this->hostEntityId)) {
$current_id = in_array($recieved_id, $this->hostEntityId) ? $recieved_id : FALSE;
}
else {
$current_id = $this->hostEntityId;
}
}
else {
$current_host = entity_revision_load($this->hostEntityType, $this->hostEntityRevisionId);
list($current_id) = $current_host ? entity_extract_ids($this->hostEntityType, $current_host) : array(
$recieved_id,
);
}
if ($current_id == $recieved_id) {
$this->hostEntity = $entity;
$delta = $this
->delta();
if (isset($entity->{$this->field_name}[$this
->langcode()][$delta]['entity'])) {
$entity->{$this->field_name}[$this
->langcode()][$delta]['entity'] = $this;
}
}
else {
throw new Exception('The host entity cannot be changed.');
}
}
public function hostEntity() {
if ($this
->fetchHostDetails()) {
if (!isset($this->hostEntity) && $this
->isInUse()) {
$this->hostEntity = entity_load_single($this->hostEntityType, $this->hostEntityId);
}
elseif (!isset($this->hostEntity) && $this->hostEntityRevisionId) {
$this->hostEntity = entity_revision_load($this->hostEntityType, $this->hostEntityRevisionId);
}
return $this->hostEntity;
}
}
public function hostEntityType() {
if ($this
->fetchHostDetails()) {
return $this->hostEntityType;
}
}
public function hostEntityId() {
if ($this
->fetchHostDetails()) {
if (!$this->hostEntityId && $this->hostEntityRevisionId) {
$this->hostEntityId = entity_id($this->hostEntityType, $this
->hostEntity());
}
return $this->hostEntityId;
}
}
public function hostEntityBundle() {
if ($entity = $this
->hostEntity()) {
list($id, $rev_id, $bundle) = entity_extract_ids($this->hostEntityType, $entity);
return $bundle;
}
}
protected function fetchHostDetails($hostEntity = NULL) {
if (!isset($this->hostEntityId) || !$this->hostEntityId && $this->hostEntityRevisionId) {
if ($this->item_id) {
$query = new EntityFieldQuery();
$field_info = $this
->fieldInfo();
$query
->fieldCondition($field_info, 'revision_id', $this->revision_id);
if ($hostEntity) {
$entity_type = key($field_info['bundles']);
$bundle = current($field_info['bundles'][$entity_type]);
$entity_info = entity_get_info($entity_type);
$key = $entity_info['entity keys']['id'];
$query
->entityCondition('entity_type', $entity_type);
$query
->entityCondition('entity_id', $hostEntity->{$key});
$query
->entityCondition('bundle', $bundle);
if (!empty($entity_info['entity keys']['language']) && !empty($entity_info['schema_fields_sql']['base table']) && in_array($entity_info['entity keys']['language'], $entity_info['schema_fields_sql']['base table'], TRUE)) {
$query
->propertyCondition($entity_info['entity keys']['language'], $hostEntity->{$entity_info['entity keys']['language']});
}
}
$query
->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
if (!$this
->isInUse()) {
$query
->age(FIELD_LOAD_REVISION);
}
$result = $query
->execute();
if ($result) {
$this->hostEntityType = key($result);
$data = current($result);
if ($this
->isInUse()) {
$data_array_keys = array_keys($data);
$this->hostEntityId = $data ? end($data_array_keys) : FALSE;
$this->hostEntityRevisionId = FALSE;
}
else {
$data_array_keys = array_keys($data);
$this->hostEntityId = FALSE;
$this->hostEntityRevisionId = $data ? end($data_array_keys) : FALSE;
}
}
else {
$this->hostEntityId = FALSE;
}
}
else {
$this->hostEntityId = FALSE;
}
}
return !empty($this->hostEntityId) || !empty($this->hostEntity) || !empty($this->hostEntityRevisionId);
}
public function delta() {
if (($entity = $this
->hostEntity()) && isset($entity->{$this->field_name})) {
foreach ($entity->{$this->field_name} as $langcode => &$data) {
if (!empty($data)) {
foreach ($data as $delta => $item) {
if (isset($item['value']) && $item['value'] == $this->item_id) {
$this->langcode = $langcode;
return $delta;
}
if (isset($item['entity']) && $item['entity'] === $this) {
$this->langcode = $langcode;
return $delta;
}
}
}
}
if (!empty($entity->original)) {
foreach ($entity->original->{$this->field_name} as $langcode => &$data) {
if (!empty($data)) {
foreach ($data as $delta => $item) {
if (isset($item['value']) && $item['value'] == $this->item_id) {
$this->langcode = $langcode;
return $delta;
}
if (isset($item['entity']) && $item['entity'] === $this) {
$this->langcode = $langcode;
return $delta;
}
}
}
}
}
}
}
public function langcode() {
if (empty($this->langcode) || $this
->delta() === NULL) {
$this->langcode = field_collection_entity_language('field_collection_item', $this);
}
if (empty($this->langcode) || $this->langcode != LANGUAGE_NONE && (!module_exists('entity_translation') || !entity_translation_enabled('field_collection_item'))) {
$this->langcode = LANGUAGE_NONE;
}
return $this->langcode;
}
public function isInUse() {
return $this->default_revision && !$this->archived;
}
public function save($skip_host_save = FALSE) {
if (!empty($this->is_new) && !(isset($this->hostEntityId) || isset($this->hostEntity) || isset($this->hostEntityRevisionId))) {
throw new Exception('Unable to create a field collection item without a given host entity.');
}
if (!empty($this->is_new) && field_collection_item_is_translatable() && $this
->langcode() == LANGUAGE_NONE) {
$this
->copyTranslations();
}
if ($skip_host_save) {
return entity_get_controller($this->entityType)
->save($this);
}
$host_entity = $this
->hostEntity();
if (!$host_entity) {
throw new Exception('Unable to save a field collection item without a valid reference to a host entity.');
}
if (!empty($this->revision) || !empty($this->is_new_revision)) {
$host_entity->revision = TRUE;
if (!empty($this->default_revision)) {
entity_revision_set_default($this->hostEntityType, $host_entity);
}
}
$delta = $this
->delta();
if (isset($delta)) {
$host_entity->{$this->field_name}[$this
->langcode()][$delta] = array(
'entity' => $this,
);
}
else {
$host_entity->{$this->field_name}[$this
->langcode()][] = array(
'entity' => $this,
);
}
return entity_save($this->hostEntityType, $host_entity);
}
public function delete($skip_host_save = FALSE) {
parent::delete();
if (!$skip_host_save) {
$this
->deleteHostEntityReference();
}
}
public function copyTranslations($source_language = NULL) {
$host_et_handler = NULL;
if (module_exists('entity_translation')) {
$host_et_handler = entity_translation_get_handler($this
->hostEntityType(), $this
->hostEntity());
}
if (is_null($host_et_handler)) {
return;
}
$host_languages = array_keys($host_et_handler
->getTranslations()->data);
if (empty($host_languages)) {
$host_languages = array(
entity_language($this
->hostEntityType(), $this
->hostEntity()),
);
}
$source_language = isset($source_language) ? $source_language : $host_et_handler
->getLanguage();
$target_languages = array_diff($host_languages, array(
$source_language,
));
$fields_instances = array_keys(field_info_instances('field_collection_item', $this->field_name));
$fields = field_info_fields();
foreach ($fields_instances as $translatable_field) {
if ($fields[$translatable_field]['translatable'] == 1) {
foreach ($target_languages as $langcode) {
if (isset($this->{$translatable_field}[$source_language]) && !isset($this->{$translatable_field}[$langcode])) {
$this->{$translatable_field}[$langcode] = $this->{$translatable_field}[$source_language];
}
}
if ($source_language == LANGUAGE_NONE && count($this->{$translatable_field}) > 1) {
$this->{$translatable_field}[$source_language] = NULL;
}
}
}
}
protected function deleteHostEntityReference() {
$delta = $this
->delta();
if ($this->item_id && isset($delta)) {
unset($this->hostEntity->{$this->field_name}[$this
->langcode()][$delta]);
if (empty($this->hostEntity->field_collection_deleting)) {
entity_save($this
->hostEntityType(), $this
->hostEntity());
}
}
}
public function deleteRevision($skip_host_update = FALSE) {
if (!$this->revision_id) {
return;
}
if (!$skip_host_update) {
$this
->deleteHostEntityReference();
}
if (!$this
->isDefaultRevision()) {
entity_revision_delete('field_collection_item', $this->revision_id);
}
else {
$row = db_select('field_collection_item_revision', 'r')
->fields('r')
->condition('item_id', $this->item_id)
->condition('revision_id', $this->revision_id, '<>')
->execute()
->fetchAssoc();
if ($row) {
db_update('field_collection_item')
->fields(array(
'archived' => 1,
'revision_id' => $row['revision_id'],
))
->condition('item_id', $this->item_id)
->execute();
entity_get_controller('field_collection_item')
->invoke('archive', $this);
entity_get_controller('field_collection_item')
->resetCache(array(
$this->item_id,
));
entity_revision_delete('field_collection_item', $this->revision_id);
}
else {
$this
->delete($skip_host_update);
}
}
}
public function export($prefix = '') {
$export = "entity_import('" . $this
->entityType() . "', '";
$export .= addcslashes(parent::export(), '\\\'');
$export .= "')";
return $export;
}
public function view($view_mode = 'full', $langcode = NULL, $page = NULL) {
$view_mode = key(entity_view_mode_prepare($this->entityType, array(
$this->item_id => $this,
), $view_mode, $langcode));
return parent::view($view_mode, $langcode, $page);
}
public function __sleep() {
$vars = get_object_vars($this);
unset($vars['entityInfo'], $vars['idKey'], $vars['nameKey'], $vars['statusKey'], $vars['fieldInfo']);
if ($this->hostEntity && ($this->hostEntityId || $this->hostEntityRevisionId)) {
unset($vars['hostEntity']);
}
return drupal_map_assoc(array_keys($vars));
}
}