/** * Builds a metadata item for a field value. * * It will add information about the referenced entity. * * @param \EntityMetadataWrapper $wrapper * The wrapper for the referenced file array. * * @return array * The metadata array item. */ protected function buildResourceMetadataItem($wrapper) { $file_array = $wrapper->value(); /* @var \EntityDrupalWrapper $wrapper */ $wrapper = entity_metadata_wrapper('file', $file_array['fid']); return parent::buildResourceMetadataItem($wrapper); }
public function isValidValue($field_name, $value, EntityMetadataWrapper $wrapper, EntityMetadataWrapper $property_wrapper) { $info = $property_wrapper->Info(); if ($info['type'] == 'field_item_image') { $value = array($value); } parent::isValidValue($field_name, $value, $wrapper, $property_wrapper); }
/** * Overrides \RestfulEntityBase::checkPropertyAccess(). * * Allow user to create a label for the unsaved term, even if the user doesn't * have access to update existing terms, as required by the entity metadata * wrapper's access check. */ protected function checkPropertyAccess($op, $public_field_name, EntityMetadataWrapper $property, EntityMetadataWrapper $wrapper) { $info = $property->info(); $term = $wrapper->value(); if (!empty($info['name']) && $info['name'] == 'name' && empty($term->tid) && $op == 'edit') { return TRUE; } return parent::checkPropertyAccess($op, $public_field_name, $property, $wrapper); }
/** * Overrides RestfulEntityBase::entityPreSave(). * * Set the node author and other defaults. */ public function entityPreSave(\EntityMetadataWrapper $wrapper) { $node = $wrapper->value(); if (!empty($node->nid)) { // Node is already saved. return; } node_object_prepare($node); $node->uid = $this->getAccount()->uid; }
/** * Get the meters list of the account. */ function getMeters(\EntityMetadataWrapper $wrapper) { $nid = $wrapper->getIdentifier(); $query = new EntityFieldQuery(); $result = $query->entityCondition('entity_type', 'node')->entityCondition('bundle', 'iec_meter')->propertyCondition('status', NODE_PUBLISHED)->fieldCondition(OG_AUDIENCE_FIELD, 'target_id', $nid)->propertyOrderBy('nid')->execute(); if (empty($result['node'])) { return; } $nids = array_keys($result['node']); $handler = restful_get_restful_handler('iec_meters'); return $handler->get(implode(',', $nids)); }
/** * Get the children of the current category item by id. */ protected function getChildren(\EntityMetadataWrapper $wrapper) { $vocabulary = taxonomy_vocabulary_machine_name_load($this->getBundle()); $children = taxonomy_get_children($wrapper->getIdentifier(), $vocabulary->vid); // Exit if there is not children. if (empty($children)) { return; } $return = array(); // Get just an array of id. foreach ($children as $term) { $return[] = $term->tid; } return $return; }
/** * Get value from a property. * * @param \EntityMetadataWrapper $property_wrapper * The property wrapper. Either \EntityDrupalWrapper or \EntityListWrapper. * * @return mixed * A single or multiple values. */ protected function fieldValue(\EntityMetadataWrapper $property_wrapper) { if ($this->getSubProperty() && $property_wrapper->value()) { $property_wrapper = $property_wrapper->{$this->getSubProperty()}; } // Wrapper method. return $property_wrapper->{$this->getWrapperMethod()}(); }
/** * Sets language of specific fields on an EntityMetadataWrapper object. * * This is essentially a copy of search_api_extract_fields(), just slightly * adapted to set language on the wrapper fields instead of extracting them. * * @param EntityMetadataWrapper $wrapper * The wrapper on which fields to set language on. * @param array $fields * The fields to set language on, as stored in an index. I.e., the array * keys are field names, the values are arrays with at least a "type" key * present. * @param array $langcode * A code of the language to set to wrapper fields. * * @return array * The $fields array with additional "value" and "original_type" keys set. * * @see SearchApiEtDatasourceController::getMetadataWrapper() * @see SearchApiEtDatasourceController::setLanguage() */ protected function setLanguage($wrapper, $fields, $langcode) { // If $wrapper is a list of entities, we have to aggregate their field values. $wrapper_info = $wrapper->info(); if (search_api_is_list_type($wrapper_info['type'])) { foreach ($fields as &$info) { $info['value'] = array(); $info['original_type'] = $info['type']; } unset($info); try { foreach ($wrapper as $w) { $nested_fields = $this->setLanguage($w, $fields, $langcode); foreach ($nested_fields as $field => $info) { if (isset($info['value'])) { $fields[$field]['value'][] = $info['value']; } if (isset($info['original_type'])) { $fields[$field]['original_type'] = $info['original_type']; } } } } catch (EntityMetadataWrapperException $e) { // Catch exceptions caused by not set list values. } return $fields; } $nested = array(); foreach ($fields as $field => $info) { $pos = strpos($field, ':'); if ($pos === FALSE) { if (isset($wrapper->{$field}) && method_exists($wrapper->{$field}, 'language')) { $wrapper->{$field}->language($langcode); } } else { list($prefix, $key) = explode(':', $field, 2); $nested[$prefix][$key] = $info; } } foreach ($nested as $prefix => $nested_fields) { if (isset($wrapper->{$prefix})) { $nested_fields = $this->setLanguage($wrapper->{$prefix}, $nested_fields, $langcode); foreach ($nested_fields as $field => $info) { $fields["{$prefix}:{$field}"] = $info; } } else { foreach ($nested_fields as &$info) { $info['value'] = NULL; $info['original_type'] = $info['type']; } } } return $fields; }
/** * Get the "self" url. * * @param \EntityMetadataWrapper $wrapper * The wrapped entity. * * @return string * The self URL. */ protected function getEntitySelf(\EntityMetadataWrapper $wrapper) { return $this->versionedUrl($wrapper->getIdentifier()); }