diff --git a/includes/WdEntityWrapper.php b/includes/WdEntityWrapper.php index d6db9ed..f585875 100644 --- a/includes/WdEntityWrapper.php +++ b/includes/WdEntityWrapper.php @@ -38,11 +38,16 @@ class WdEntityWrapper { * Entity to wrap. Will load entity if ID is passed. */ public function __construct($entity_type, $entity) { + global $language_content; + if (is_numeric($entity)) { $entity = entity_load_single($entity_type, $entity); } $this->entity = entity_metadata_wrapper($entity_type, $entity); $this->entity_type = $entity_type; + + // Set the language to the current content language. + $this->entity->language($language_content->prefix); } /** diff --git a/includes/WdFieldFormatter.php b/includes/WdFieldFormatter.php new file mode 100644 index 0000000..584b35d --- /dev/null +++ b/includes/WdFieldFormatter.php @@ -0,0 +1,263 @@ +property = $property; + + if ($isMultivalue) { + // It is already an array. + $this->value = $this->property->value(); + } + else { + // Build an array with one item. + $this->value = array($this->property->value()); + } + + $this->isMultivalue = $isMultivalue; + } + + /** + * Returns the current field item as a raw array. + */ + public function value() { + return $this->value[$this->delta]; + } + + /** + * Returns all raw field items in an array. + */ + public function values() { + return $this->value; + } + + /** + * Set the current field delta. + */ + public function setDelta($delta) { + if ($this->isDeltaValid($delta)) { + $this->delta = $delta; + } + else { + // TODO warning. delta is not valid. + } + + return $this; + } + + /** + * Checks if the given or current field delta exists. + * + * @param null $delta + * @return bool + */ + public function isDeltaValid($delta = NULL) { + $delta = empty($delta) ? $this->delta : $delta; + + return isset($this->value[$delta]); + } + + /** + * Implementing IteratorAggregate interface. + * + * If we wrap a list, we return an iterator over the data list. + */ + public function getIterator() { + return new WdFieldFormatterIterator($this); + } + + /** + * Implementing Countable interface. + * + * @return int The number of items of this field. + */ + public function count() { + return count($this->value); + } +} + +/** + * Allows to easily iterate over field items. + */ +class WdFieldFormatterIterator implements Iterator { + + protected $position = 0; + protected $wrapper; + + public function __construct(WdFieldFormatter $wrapper) { + $this->wrapper = $wrapper; + } + + function rewind() { + $this->position = 0; + $this->wrapper->setDelta($this->position); + } + + function current() { + return $this->wrapper; + } + + function key() { + return $this->position; + } + + function next() { + $this->position++; + $this->wrapper->setDelta($this->position); + } + + function valid() { + return $this->wrapper->isDeltaValid($this->position); + } + +} + +class WdTextFormatter extends WdFieldFormatter { + protected $truncate = 300; + + public function setTruncate($truncate) { + $this->truncate = $truncate; + return $this; + } + + public function text() { + $item = $this->value[$this->delta]; + if (is_array($item)) { + return $item['safe_value']; + } + else { + return $item; + } + } + + public function summary($truncate = 0) { + $item = $this->value[$this->delta]; + $text = $this->text(); + + if (!empty($item['safe_summary'])) { + return $item['safe_summary']; + } + else { + $format = isset($item['format']) ? $item['format'] : NULL; + $trimLength = $truncate > 0 ? $truncate : $this->truncate; + return text_summary($text, $format, $trimLength); + } + } + +} + +class WdImageFormatter extends WdFieldFormatter { + protected $imageStyle; + + public function setImageStyle($imageStyle) { + $this->imageStyle = $imageStyle; + return $this; + } + + public function src() { + if (!$this->isDeltaValid()) { + return; + } + + if ($this->imageStyle) { + return image_style_url($this->imageStyle, $this->value[$this->delta]['uri']); + } + else { + return file_create_url($this->value[$this->delta]['uri']); + } + } + + public function img() { + if (!$this->isDeltaValid()) { + return; + } + + $variables = $this->value[$this->delta]; + + $variables += array( + 'style_name' => $this->imageStyle, + 'path' => $this->value[$this->delta]['uri'], + ); + + if ($this->imageStyle) { + return theme_image_style($variables); + } + else { + return theme_image($variables); + } + } +} + + + +class WdEntityReferenceFormatter extends WdFieldFormatter { + protected $viewMode = 'full'; + protected $entityType; + + public function __construct($property, $isMultivalue) { + parent::__construct($property, $isMultivalue); + + $type = $this->property->type(); + if (entity_property_list_extract_type($type)) { + $this->entityType = entity_property_list_extract_type($type); + } + else { + $this->entityType =$type; + } + + } + + public function setViewMode($viewMode) { + $this->viewMode = $viewMode; + return $this; + } + + /** + * Render a node with a view mode. + * + * @return string + */ + public function render() { + dpm($this->property->type()); + $render_array = entity_view($this->entityType, array($this->value[$this->delta]), $this->viewMode); + return drupal_render($render_array); + } + + public function path() { + $path = entity_uri($this->entityType, $this->value[$this->delta]); + return $path['path']; + } + + public function url($options = array()) { + return url($this->path(), $options); + } + + public function label() { + return entity_label($this->entityType, $this->value[$this->delta]); + } + + /** + * Get the node title wrapped in a link. + * + * @return string + */ + public function linkedLabel() { + return l($this->label(), $this->path()); + } +} + + diff --git a/templates/methods/formatterFIELD.txt b/templates/methods/formatterFIELD.txt new file mode 100644 index 0000000..e8820d8 --- /dev/null +++ b/templates/methods/formatterFIELD.txt @@ -0,0 +1,8 @@ + /** + * Retrieves a Formatter Class for {{ field_name }} + * + * @return {{ formatter_class }} + */ + public function formatter{{ field_name_camelized }}() { + return new {{ formatter_class }}($this->entity->get('{{ field_name }}'), {{ formatter_multivalue }}); + } \ No newline at end of file diff --git a/wrappers_delight.info b/wrappers_delight.info index 91b0725..98baa1e 100644 --- a/wrappers_delight.info +++ b/wrappers_delight.info @@ -4,6 +4,7 @@ package = Wrappers Delight core = 7.x files[] = includes/WdEntityWrapper.php +files[] = includes/WdFieldFormatter.php files[] = includes/WdNodeWrapper.php files[] = includes/WdUserWrapper.php files[] = includes/WdWrapperQuery.php diff --git a/wrappers_delight.module b/wrappers_delight.module index d4abf62..0f82a9e 100644 --- a/wrappers_delight.module +++ b/wrappers_delight.module @@ -280,6 +280,9 @@ function wrappers_delight_build_method_from_template($template_file, $field_info $replacements['target_entity_class'] = wrappers_delight_get_parent_class($field_info['settings']['target_type']); } + $replacements['formatter_class'] = wrappers_delight_get_formatter_class($field_info); + $replacements['formatter_multivalue'] = ($field_info['cardinality'] > 1 || $field_info['cardinality'] == -1) ? 'TRUE' : 'FALSE'; + $method = wrappers_delight_replace_tokens($replacements, $method); return $method; } @@ -364,7 +367,7 @@ function wrappers_delight_generate_class_name($entity_type, $bundle) { } /** - * Returns the best avaiable wrapper class for a given entity type and bundle. + * Returns the best available wrapper class for a given entity type and bundle. * * @param $entity_type * @param $bundle @@ -385,6 +388,47 @@ function wrappers_delight_get_wrapper_class($entity_type, $bundle) { return 'WdEntityWrapper'; } + +/** + * Returns the best available formatter class for a given field type. + * + * @param $field_info + * @return string + * The determined formatter class + */ +function wrappers_delight_get_formatter_class($field_info) { + + switch($field_info['type']) { + case 'text': + case 'text_long': + case 'text_with_summary': + $formatter_class = 'WdTextFormatter'; + break; + + case 'image': + $formatter_class = 'WdImageFormatter'; + break; + + case 'entityreference': + case 'taxonomy_term_reference': + $formatter_class = 'WdEntityReferenceFormatter'; + break; + + case 'datetime': + $formatter_class = 'WdTextFormatter'; + break; + + case 'field_collection': + $formatter_class = 'WdEntityReferenceFormatter'; + break; + + default: + $formatter_class = 'WdFieldFormatter'; + break; + } + return $formatter_class; +} + /** * Increment the version number in an .info file. *