/**
  * 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);
 }
 /**
  * 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;
 }
Beispiel #3
0
  /**
   * Get the "target_type" property from an field or property reference.
   *
   * @param \EntityMetadataWrapper $wrapper
   *   The wrapped property.
   * @param $property
   *   The public field name.
   *
   * @return string
   *   The target type of the referenced entity.
   *
   * @throws \RestfulException
   */
  protected function getTargetTypeFromEntityReference(\EntityMetadataWrapper $wrapper, $property) {
    $params = array('@property' => $property);

    if ($field = field_info_field($property)) {
      if ($field['type'] == 'entityreference') {
        return $field['settings']['target_type'];
      }
      elseif ($field['type'] == 'taxonomy_term_reference') {
        return 'taxonomy_term';
      }
      elseif ($field['type'] == 'field_collection') {
        return 'field_collection_item';
      }
      elseif ($field['type'] == 'commerce_product_reference') {
        return 'commerce_product';
      }
      elseif ($field['type'] == 'commerce_line_item_reference') {
        return 'commerce_line_item';
      }

      throw new \RestfulException(format_string('Field @property is not an entity reference or taxonomy reference field.', $params));
    }
    else {
      // This is a property referencing another entity (e.g. the "uid" on the
      // node object).
      $info = $wrapper->info();
      if ($this->getEntityInfo($info['type'])) {
        return $info['type'];
      }

      throw new \RestfulException(format_string('Property @property is not defined as reference in the EntityMetadataWrapper definition.', $params));
    }
  }