/** * {@inheritdoc} * * @throws \EntityMetadataWrapperException */ public function access($op, DataInterpreterInterface $interpreter) { // Perform basic access checks. if (!$this->decorated->access($op, $interpreter)) { return FALSE; } if (!$this->getProperty()) { // If there is no property we cannot check for property access. return TRUE; } // Perform field API access checks. if (!($property_wrapper = $this->propertyWrapper($interpreter))) { return FALSE; } if ($this->isWrapperMethodOnEntity() && $this->getWrapperMethod() && $this->getProperty()) { // Sometimes we define fields as $wrapper->getIdentifier. We need to // resolve that to $wrapper->nid to call $wrapper->nid->info(). $property_wrapper = $property_wrapper->{$this->getProperty()}; } $account = $interpreter->getAccount(); // Check format access for text fields. if ($op == 'edit' && $property_wrapper->type() == 'text_formatted' && $property_wrapper->value() && $property_wrapper->format->value()) { $format = (object) array('format' => $property_wrapper->format->value()); // Only check filter access on write contexts. if (!filter_access($format, $account)) { return FALSE; } } $info = $property_wrapper->info(); if ($op == 'edit' && empty($info['setter callback'])) { // Property does not allow setting. return FALSE; } // If $interpreter->getWrapper()->value() === FALSE it means that the entity // could not be loaded, thus checking properties on it will result in // errors. // Ex: this happens when the embedded author is the anonymous user. Doing // user_load(0) returns FALSE. $access = $interpreter->getWrapper()->value() !== FALSE && $property_wrapper->access($op, $account); return $access !== FALSE; }
/** * Define custom prepare_translation behavior for this module's field types. * * @param $entity_type * The type of $entity. * @param $entity * The entity for the operation. * @param $field * The field structure for the operation. * @param $instance * The instance structure for $field on $entity's bundle. * @param $langcode * The language associated to $items. * @param $items * $entity->{$field['field_name']}[$langcode], or an empty array if unset. * @param $source_entity * The source entity from which field values are being copied. * @param $source_langcode * The source language from which field values are being copied. */ function hook_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) { // If the translating user is not permitted to use the assigned text format, // we must not expose the source values. $field_name = $field['field_name']; $formats = filter_formats(); $format_id = $source_entity->{$field_name}[$source_langcode][0]['format']; if (!filter_access($formats[$format_id])) { $items = array(); } }
/** * Check access on a property. * * @param string $op * The operation that access should be checked for. Can be "view" or "edit". * Defaults to "edit". * @param string $public_field_name * The name of the public field. * @param EntityMetadataWrapper $property_wrapper * The wrapped property. * @param EntityMetadataWrapper $wrapper * The wrapped entity. * * @return bool * TRUE if the current user has access to set the property, FALSE otherwise. */ protected function checkPropertyAccess($op, $public_field_name, EntityMetadataWrapper $property_wrapper, EntityMetadataWrapper $wrapper) { if (!$this->checkPropertyAccessByAccessCallbacks($op, $public_field_name, $property_wrapper, $wrapper)) { // Access callbacks denied access. return; } $account = $this->getAccount(); // Check format access for text fields. if ($property_wrapper->type() == 'text_formatted' && $property_wrapper->value() && $property_wrapper->format->value()) { $format = (object) array('format' => $property_wrapper->format->value()); // Only check filter access on write contexts. if (\RestfulBase::isWriteMethod($this->getMethod()) && !filter_access($format, $account)) { return FALSE; } } $info = $property_wrapper->info(); if ($op == 'edit' && empty($info['setter callback'])) { // Property does not allow setting. return FALSE; } $access = $property_wrapper->access($op, $account); return $access !== FALSE; }