protected function getParentEntitySettings(EntityInterface $embed, ListAttribute $parent_attribute) { $parent_entity_settings = []; $view_scope = $this->getViewScope(); $view_config_service = $this->getServiceLocator()->getViewConfigService(); $parent_entity = $embed->getParent(); if ($parent_entity) { // settings specified on parent entity $entity_renderer_config = $view_config_service->getRendererConfig($view_scope, $this->getOutputFormat(), $parent_entity); $all_fields_options = $entity_renderer_config->get('__all_fields', new ArrayConfig([])); $specific_fields_options = $entity_renderer_config->get('__fields_options', new ArrayConfig([])); $parent_field_options = $specific_fields_options->get('field_' . $parent_attribute->getName(), new ArrayConfig([])); $parent_entity_settings = array_replace_recursive($all_fields_options->toArray(), $parent_field_options->toArray()); } return new ArrayConfig($parent_entity_settings); }
protected function getRenderedFields(EntityInterface $entity, $view_template) { $rendered_fields = []; $entity_type = $entity->getType(); $fields = $view_template->extractAllFields(); $fields_options = $this->getOption('__fields_options', new Settings()); $all_fields_options = $fields_options->get('__all_fields', new Settings())->toArray(); foreach ($fields as $field_name => $field) { $field_settings = $field->getConfig(); $attribute = null; if ($field_settings->has('attribute_path')) { $attribute = $entity_type->getAttribute($field_settings->get('attribute_path')); } $default_render_settings = ['group_parts' => $this->getOption('group_parts', [$entity_type->getPrefix()]), 'field_name' => $field->getName(), 'view_scope' => $this->getOption('view_scope'), 'is_within_embed_template' => $this->getOption('is_embed_template', false), 'readonly' => $this->getOption('readonly', false)]; $field_renderer_config = $this->view_config_service->getRendererConfig($this->getOption('view_scope', 'missing.view_scope'), $this->output_format, 'field_' . $field->getName())->toArray(); if ($field_settings->has('renderer')) { $field_renderer_config['renderer'] = $field_settings->get('renderer'); } $attribute_type_renderer_config = []; if ($attribute) { $attribute_type_renderer_config = $this->view_config_service->getRendererConfig($this->getOption('view_scope', 'missing.view_scope'), $this->output_format, $attribute)->toArray(); } $renderer_config = array_replace_recursive($attribute_type_renderer_config, $field_renderer_config); $render_settings = array_replace_recursive($default_render_settings, $renderer_config, $field_settings->toArray(), $all_fields_options, $fields_options->get($field_name, new Settings())->toArray()); $renderer_config = new ArrayConfig($renderer_config); if ($attribute) { $renderer = $this->renderer_service->getRenderer($attribute, $this->output_format, $renderer_config); $rendered_field = $renderer->render(['attribute' => $attribute, 'resource' => $entity], $render_settings); } else { if (!$renderer_config->has('renderer')) { throw new RuntimeError(sprintf('When no "attribute_path" is given a "renderer" setting is mandatory on ' . 'field "%s" in view template "%s" of view scope "%s" for type "%s".', $field->getName(), $view_template->getName(), $this->getOption('view_scope', ''), $entity->getType()->getPrefix())); } $renderer = $this->renderer_service->getRenderer(null, $this->output_format, $renderer_config); $rendered_field = $renderer->render(['resource' => $entity], $render_settings); } // todo index should be tab->panel->row->item->group->field instead of field only // as the same field could be rendered in different positions with different css // on the other hand fields can have unique names and still use the same attribute(-path) if (isset($rendered_fields[$field->getName()])) { throw new RuntimeError(sprintf('Field "%s" defined multiple times. Please rename the field ' . 'in view template "%s" of view scope "%s" for type "%s".', $field->getName(), $view_template->getName(), $this->getOption('view_scope', ''), $entity->getType()->getPrefix())); } $rendered_fields[$field->getName()] = $rendered_field; } return $rendered_fields; }
/** * Create a new entity from the recursively mirrorred values of a given source entity, while * optionally merging attribute values from a given reference entity. * * @param EntityInterface $source_entity * @param EntityInterface $reference_entity * * @return EntityInterface */ public function createMirroredEntity(EntityInterface $source_entity, EntityInterface $reference_entity = null) { // compile non-list attribute values from the reference entity if available if ($reference_entity) { foreach ($this->getAttributes() as $attribute) { $attribute_name = $attribute->getName(); $attribute_value = $reference_entity->getValue($attribute_name); $mirrored_values[$attribute_name] = $attribute_value instanceof ObjectInterface ? $attribute_value->toArray() : $attribute_value; } } // override default mirrored values $mirrored_values['@type'] = $source_entity->getType()->getPrefix(); $mirrored_values['identifier'] = $source_entity->getIdentifier(); if ($source_entity instanceof EntityReferenceInterface) { $mirrored_values['referenced_identifier'] = $source_entity->getReferencedIdentifier(); } // collate the required mirrored attributes map $mirrored_attributes_map = $this->collateAttributes(function (AttributeInterface $attribute) { return (bool) $attribute->getOption('mirrored', false) === true; }); // extract our reference path which may be aliased $path_parts = explode('.', $this->getPrefix()); $type_prefix = end($path_parts); // iterate the source attributes and extract the required mirrored values foreach ($mirrored_attributes_map->getKeys() as $mirrored_attribute_path) { // @todo possible risk of path name collision in greedy regex $mirrored_attribute_path = preg_replace('#([\\w-]+\\.)+' . $type_prefix . '\\.#', '', $mirrored_attribute_path); $mirrored_attr_name = explode('.', $mirrored_attribute_path)[0]; $mirrored_attribute = $this->getAttribute($mirrored_attr_name); $source_attribute_name = $mirrored_attribute->getOption('attribute_alias', $mirrored_attr_name); $source_attribute_value = $source_entity->getValue($source_attribute_name); if ($mirrored_attribute instanceof EmbeddedEntityListAttribute) { foreach ($source_attribute_value as $position => $source_embedded_entity) { // skip entity mirroring if values already exist since we may traverse over paths repeatedly // if (!isset($mirrored_values[$mirrored_attr_name][$position])) { // 2016-09-28 shrink0r: when would this happen. commenting out if check, // as this seems to work fine without. $source_embed_prefix = $source_embedded_entity->getType()->getPrefix(); $mirrored_embed_type = $mirrored_attribute instanceof EntityReferenceListAttribute ? $mirrored_attribute->getEmbeddedTypeByReferencedPrefix($source_embed_prefix) : $mirrored_attribute->getEmbeddedTypeByPrefix($source_embed_prefix); if ($mirrored_embed_type) { $reference_embedded_entity = $reference_entity ? $reference_entity->getValue($mirrored_attr_name)->getEntityByIdentifier($source_embedded_entity->getIdentifier()) : null; $mirrored_embedded_entity = $mirrored_embed_type->createEntity($mirrored_embed_type->createMirroredEntity($source_embedded_entity, $reference_embedded_entity)->toArray(), $reference_entity); $mirrored_values[$mirrored_attr_name][$position] = $mirrored_embedded_entity->toArray(); } } } else { $mirrored_values[$mirrored_attr_name] = $source_attribute_value instanceof ObjectInterface ? $source_attribute_value->toArray() : $source_attribute_value; } } return $this->createEntity($mirrored_values, $source_entity->getParent()); }
protected function filterUnmodifiedValues(EntityInterface $entity, array $payload) { $modified_values = []; foreach ($entity->getType()->getAttributes() as $attribute_name => $attribute) { if (!array_key_exists($attribute_name, $payload)) { continue; } $value_holder = $attribute->createValueHolder(); $payload_value = $payload[$attribute_name]; $attribute_value = $entity->getValue($attribute_name); $result = $value_holder->setValue($payload_value, $entity); if ($result->getSeverity() <= IncidentInterface::NOTICE) { if (!$value_holder->sameValueAs($attribute_value)) { $modified_values[$attribute_name] = $value_holder->toNative(); } } else { error_log(sprintf('[%s] Invalid values given for "%s": %s', __METHOD__, $attribute->getPath(), var_export($payload_value, true))); } } return $modified_values; }
protected function getGlanceImage(EntityInterface $resource, ViewTemplateInterface $view_template) { $image_default_attributes = ['location' => '', 'width' => 100, 'height' => 100]; // @todo 'image_url' should allow to restore the auto-retriving without specifying 'image_value_path', // in the case a value was already set for it in a less specific config. if ($this->hasOption('image_url')) { $image_url = $this->getOption('image_url'); if (!empty($image_url) || !$this->hasOption('image_value_path')) { // empty value would reset eventual global options and allow to use the value_path option return array_replace($image_default_attributes, ['location' => $image_url]); } } $converjon_enabled = (bool) $this->getOption('use_converjon', false); $image_activity = null; if ($converjon_enabled) { $image_activity = $this->activity_service->getActivity($this->getOption('image_activity_scope', 'converjon'), $this->getOption('image_activity_name', 'thumbnail')); $image_activity_url = $image_activity->getUrl(); $image_default_attributes['width'] = $image_activity_url->getParameter('width'); $image_default_attributes['height'] = $image_activity_url->getParameter('height'); } // get value from configured value path if ($this->hasOption('image_value_path')) { $path = $this->getOption('image_value_path'); $image_attribute = AttributePath::getAttributeByPath($resource->getType(), $path); if ($image_attribute instanceof HandlesFileListInterface && $image_attribute->getFiletypeName() === HandlesFileInterface::FILETYPE_IMAGE) { $image_value = AttributeValuePath::getAttributeValueByPath($resource, $path); $index = $this->getOption('image_value_path_index', 0); if (array_key_exists($index, $image_value)) { $image = $image_value[$index]->toNative(); $location = $image[$image_attribute->getFileLocationPropertyName()]; if ($converjon_enabled) { $image_url = $this->url_generator->generateUrl($image_activity, ['file' => $location]); return array_replace($image_default_attributes, ['location' => $image_url]); } else { $image_url = $this->url_generator->generateUrl('module.files.download', ['resource' => $resource, 'file' => $location]); return array_replace($image_default_attributes, ['location' => $image_url]); } } } else { throw new RuntimeError('Not supported at the moment. Please implement me.'); } } // otherwise figure out a fallback value if there are attributes containing images $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 ($attribute instanceof HandlesFileInterface && $attribute->getFiletypeName() === HandlesFileInterface::FILETYPE_IMAGE) { $image_value = $resource->getValue($attribute->getName()); if ($attribute instanceof HandlesFileListInterface) { if (array_key_exists(0, $image_value)) { $image = $image_value[0]->toNative(); $location = $image[$attribute->getFileLocationPropertyName()]; if ($converjon_enabled) { $image_url = $this->url_generator->generateUrl($image_activity, ['file' => $location]); return array_replace($image_default_attributes, ['location' => $image_url]); } else { $image_url = $this->url_generator->generateUrl('module.files.download', ['resource' => $resource, 'file' => $location]); return array_replace($image_default_attributes, ['location' => $image_url]); } } } elseif (!empty($image_value)) { $image = $image_value->toNative(); $location = $image[$attribute->getFileLocationPropertyName()]; if ($converjon_enabled) { $image_url = $this->url_generator->generateUrl($image_activity, ['file' => $location]); return array_replace($image_default_attributes, ['location' => $image_url]); } else { $image_url = $this->url_generator->generateUrl('module.files.download', ['resource' => $resource, 'file' => $location]); return array_replace($image_default_attributes, ['location' => $image_url]); } } } } } return $image_default_attributes; }