/** * Overrides RestfulEntityBase::getFormSchemaAllowedValues(). * * For OG vocab fields we get only the ones of the group passed context. */ protected function getFormSchemaAllowedValues($field) { if ($field['field_name'] == 'c4m_related_document') { // We don't need allowed values for related documents, because we are // getting them from library. return array(); } if ($field['field_name'] != OG_VOCAB_FIELD) { return parent::getFormSchemaAllowedValues($field); } $request = $this->getRequest(); if (empty($request['group']) || !intval($request['group'])) { throw new \RestfulBadRequestException('The "group" parameter is missing for the request, thus the vocabulary cannot be set for the group.'); } $node = node_load($request['group']); if (!$node) { throw new \RestfulBadRequestException('The "group" parameter is not a node.'); } elseif ($node->type != 'group') { throw new \RestfulBadRequestException('The "group" parameter is not a of type "group".'); } $return = array(); foreach (array('tags', 'categories') as $vocab_name) { $allowed_values = array(); $og_vocab = c4m_restful_get_og_vocab_by_name('node', $node->nid, $vocab_name); $vocab = taxonomy_vocabulary_load($og_vocab[0]->vid); $allowed_values[] = array('vocabulary' => $vocab->machine_name, 'parent' => 0); $field['settings']['allowed_values'] = $allowed_values; $return[$vocab_name] = taxonomy_allowed_values($field); } return $return; }
/** * {@inheritdoc} * * Change the bundle on the fly, based on a parameter send in the request. * This applies only for the "tags" and "categories" resources. */ public function process($path = '', array $request = array(), $method = \RestfulInterface::GET, $check_rate_limit = TRUE) { $resource_name = $this->getResourceName(); if ($resource_name == 'tags' || $resource_name == 'categories') { if (empty($request['group']) || !intval($request['group'])) { throw new \RestfulBadRequestException('The "group" parameter is missing for the request, thus the vocabulary cannot be set for the group.'); } $node = node_load($request['group']); if (!$node) { throw new \RestfulBadRequestException('The "group" parameter is not a node.'); } elseif ($node->type != 'group') { throw new \RestfulBadRequestException('The "group" parameter is not a of type "group".'); } $vocab_name = $resource_name == 'tags' ? 'Tags' : 'Categories'; if (!($og_vocab = c4m_restful_get_og_vocab_by_name('node', $node->nid, $vocab_name))) { throw new \RestfulBadRequestException('The "group" does not have a "' . $vocab_name . '" vocabulary.'); } $this->bundle = $og_vocab[0]->machine_name; // Group ID removed from the request because it's not a field in the // taxonomy. unset($request['group']); } return parent::process($path, $request, $method, $check_rate_limit); }