protected function doRender() { if ($this->hasOption('value')) { return $this->getOption('value'); } $resource = $this->getPayload('resource'); $root_resource = $resource->getRoot() ?: $resource; $value_path = $this->getOption('attribute_value_path'); if (!empty($value_path)) { $assets = AttributeValuePath::getAttributeValueByPath($resource, $value_path); } else { $assets = $resource->getValue($this->attribute->getName()); } $json_data = []; foreach ($assets as $asset) { $asset_data = $asset->toArray(); $download_url = $this->url_generator->generateUrl('module.files.download', ['resource' => $root_resource, 'file' => $asset->getLocation()]); $asset_data['download_url'] = $download_url; foreach ((array) $this->getOption('exclude_properties', []) as $property) { unset($asset_data[$property]); } $json_data[] = $asset_data; } return $json_data; }
public function executeRead(AgaviRequestDataHolder $request_data) { $list_config = $request_data->getParameter('list_config'); $resource_type = $this->getProjectionType(); $data_access_service = $this->getServiceLocator()->getDataAccessService(); $query_service_map = $data_access_service->getQueryServiceMap(); $query_service = $query_service_map->getByProjectionType($this->getProjectionType()); $query_result = $query_service->find($list_config->asQuery()); $display_attribute_names = $request_data->getParameter('display_fields'); foreach ($display_attribute_names as $display_attribute_name) { if (!$resource_type->hasAttribute($display_attribute_name)) { throw new RuntimeError(sprintf('Non existant display_field "%s" given for type %s', $display_attribute_name, $resource_type->getName())); } } $suggestions = []; foreach ($query_result->getResults() as $resource) { $suggestion = ['identifier' => $resource->getIdentifier()]; foreach ($display_attribute_names as $display_attribute_name) { $suggestion[$display_attribute_name] = AttributeValuePath::getAttributeValueByPath($resource, $display_attribute_name); } $suggestions[] = $suggestion; } $this->setAttribute('resource_type', $resource_type); $this->setAttribute('suggestions', $suggestions); $this->setAttribute('view_scope', $this->getScopeKey()); return 'Success'; }
protected function fetchEmbeddedEntity(EntityInterface $root_entity, $embed_path) { try { return AttributeValuePath::getAttributeValueByPath($root_entity, $embed_path); } catch (RuntimeException $error) { return null; } }
public function testGetNestedValue() { $type = new ArticleType(); $entity = $type->createEntity(['headline' => 'hel', 'content_objects' => [['@type' => 'paragraph', 'title' => 'this is a paragraph', 'text' => 'wat?!', 'coords' => ['lon' => 12.34, 'lat' => 56.78]]]]); $paragraph = $entity->getValue('content_objects')->getFirst(); $gp = $paragraph->getValue('coords'); $this->assertInstanceOf(GeoPoint::CLASS, $gp); $this->assertEquals(12.34, $gp->getLongitude()); $gp = AttributeValuePath::getAttributeValueByPath($entity, 'content_objects.*[0].coords'); $this->assertInstanceOf(GeoPoint::CLASS, $gp); $this->assertEquals(56.78, $gp->getLatitude()); }
protected function buildInitialWidgetValue(array $suggest_options) { $widget_value = []; foreach ($this->determineAttributeValue($this->attribute->getName()) as $embedded_entity) { $embedded_type = $embedded_entity->getType()->getPrefix(); if (!isset($suggest_options[$embedded_type])) { throw new RuntimeError('Missing suggest configuration for embed-type: ' . $embedded_type); } $embedded_type_opts = $suggest_options[$embedded_type]; $suggest_field = $embedded_type_opts['suggest_field']; $label_field = $embedded_type_opts['label_field'] ?: $suggest_field; $widget_value[] = ['value' => $embedded_entity->getValue(self::SUGGEST_VALUE_ATTRIBUTE), 'label' => AttributeValuePath::getAttributeValueByPath($embedded_entity, $label_field)]; } return $widget_value; }
protected function getSuggestions() { $display_attribute_names = $this->getAttribute('display_fields', []); $resource_type = $this->getAttribute('resource_type'); $query_result = $this->getAttribute('query_result'); foreach ($display_attribute_names as $display_attribute_name) { if (!$resource_type->hasAttribute($display_attribute_name)) { throw new RuntimeError(sprintf('Non existant display_field "%s" given for type %s', $display_attribute_name, $resource_type->getName())); } } $suggestions = []; foreach ($query_result->getResults() as $resource) { $suggestion = ['identifier' => $resource->getIdentifier()]; foreach ($display_attribute_names as $display_attribute_name) { $suggestion[$display_attribute_name] = AttributeValuePath::getAttributeValueByPath($resource, $display_attribute_name); } $suggestions[$resource->getIdentifier()] = $suggestion; } return $suggestions; }
protected function doRender() { $value = null; if ($this->hasOption('value')) { return $this->getOption('value'); } $value_path = $this->getOption('attribute_value_path'); if (!empty($value_path)) { $value = AttributeValuePath::getAttributeValueByPath($this->getPayload('resource'), $value_path); } else { $value = $this->getPayload('resource')->getValue($this->attribute->getName()); } if (is_object($value)) { if ($value instanceof DateTimeInterface) { $value = $value->format(TimestampAttribute::FORMAT_ISO8601); } elseif ($value instanceof ComplexValueInterface) { $value = $value->toArray(); } else { // unknown object, hopefully json serializable } } if ($this->attribute instanceof ListAttribute) { if (is_array($value)) { return array_map(function ($elm) { if ($elm instanceof ComplexValueInterface) { return $elm->toArray(); } return $elm; }, $value); } elseif ($value instanceof EntityList) { $rendered_entities = []; foreach ($value as $entity) { $rendered_entities[] = $this->renderer_service->renderSubject($entity, $this->output_format, new ArrayConfig($this->getOptions())); } return $rendered_entities; } else { return $value; } } return $value; }
protected function doRender() { if ($this->hasOption('value')) { return $this->getOption('value'); } $resource = $this->getPayload('resource'); $root_resource = $resource->getRoot() ?: $resource; $value_path = $this->getOption('attribute_value_path'); if (!empty($value_path)) { $value = AttributeValuePath::getAttributeValueByPath($resource, $value_path); } else { $value = $resource->getValue($this->attribute->getName()); } $rendered_entities = []; foreach ($value as $entity) { $rendered_entity = $this->renderer_service->renderSubject($entity, $this->output_format, new ArrayConfig($this->getOptions())); if (!is_array($rendered_entity)) { throw new RuntimeError('Rendered entity should be an array for haljson rendering.'); } if (isset($rendered_entity['@type'])) { $referenced_type_class = $this->attribute->getEmbeddedTypeByReferencedPrefix($rendered_entity['@type'])->getReferencedTypeClass(); $art = $this->resource_type_map->getByClassName($referenced_type_class); $view_resource_url = $this->url_generator->generateUrl('module.resource', ['module' => $art, 'resource' => $rendered_entity['referenced_identifier']]); $rendered_entity['_links'] = []; $link = ['href' => $view_resource_url, 'name' => 'view_resource']; $rendered_entity['_links']['honeybee:' . $art->getPrefix() . '~view_resource'] = $link; } else { $this->logger->error('No @type in reference data: ' . var_export($rendered_entity, true)); } foreach ((array) $this->getOption('exclude_properties', []) as $property) { unset($rendered_entity[$property]); } $rendered_entities[] = $rendered_entity; } return $rendered_entities; }
protected function determineAttributeValue($attribute_name) { $value = ''; if ($this->hasOption('value')) { return $this->getOption('value'); } $value_path = $this->getOption('attribute_value_path'); if (!empty($value_path)) { $value = AttributeValuePath::getAttributeValueByPath($this->getPayload('resource'), $value_path); } else { $value = $this->getPayload('resource')->getValue($attribute_name); } // @todo introduce nested rendering or smarter mechanisms or error message for resources and other known types? if (is_object($value)) { if ($value instanceof DateTimeInterface) { $value = $value->format(TimestampAttribute::FORMAT_ISO8601); } elseif (!$value instanceof ComplexValueInterface) { $value = sprintf('Attribute "%s" (type "%s") – value of type object (%s): %s', $attribute_name, get_class($this->attribute), get_class($value), StringToolkit::getObjectAsString($value)); } else { // it's a complex value so we should not convert the object to string or similar // the specific renderer for that attribute might want to use the actual object } } elseif (is_array($value)) { $value = sprintf('Attribute "%s" – value of type array with keys: %s', $attribute_name, print_r(array_keys($value), true)); } else { // $value = StringToolkit::getAsString($value); } return $value; }
protected function getGlanceDescription(EntityInterface $resource, ViewTemplateInterface $view_template) { if ($this->hasOption('description')) { $description = $this->getOption('description'); // empty value would reset eventual global options and allow to use the value_path option if ($this->hasOption('description_value_path') && empty($description)) { return AttributeValuePath::getAttributeValueByPath($resource, $this->getOption('description_value_path')); } else { return $description; } } // otherwise get first textarea attribute value $view_template_fields = $view_template->extractAllFields(); foreach ($view_template_fields as $field) { $attribute_path = $field->getSetting('attribute_path'); if ($attribute_path) { $attribute = AttributePath::getAttributeByPath($resource->getType(), $attribute_path); if (in_array(get_class($attribute), [TextareaAttribute::class])) { return AttributeValuePath::getAttributeValueByPath($resource, $attribute_path); } } } return ''; }
/** * @dataProvider articleValuePathProvider */ public function testFoo($article, $value_path, $expected_value) { $value = AttributeValuePath::getAttributeValueByPath($article, $value_path); $this->assertEquals($expected_value, $value); }