Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions

Only show the image from $content


Reply

3 Posts   1032 Views

Avatar
jeroeng

22 February 2010 at 11:29pm (Last edited: 22 February 2010 11:41pm), Community Member, 2 Posts

Hi there,

first, i'm new to silverstripe, i want to use it for a simple website for my client.

The website has 2 templates;

1) list of items ($title)
2) item overview ($title, $content)

The content of an item is text, and several images (added with TinyMCE in the content field).

Nothing really excited, and got it working in a few hours.

But now the problem/question; I want to include an image as a thumbnail in the list-view. Is it possible to extract the first image (or even a random) that is inserted in the $content and show it as a thumbnail in the list view?

For example $content->$image(1) ?

Avatar
banal

23 February 2010 at 1:52am Community Member, 901 Posts

Hi and welcome to the Forums

If I were you, I would add the images using a one-to-many relation.
This can easily be done using the DataObjectManager module. Since you would then have the images as separate DataObjects, it would be much simpler to get one of them.
See: http://doc.silverstripe.org/doku.php?id=modules:dataobjectmanager
http://silverstripe.org/dataobjectmanager-module-forum/

Otherwise you would have to parse the HTML Content and search for images there. Also possible, but not very nice.

Avatar
jeroeng

23 February 2010 at 2:03am (Last edited: 23 February 2010 2:09am), Community Member, 2 Posts

I managed to get it working with a one-to-many, and it's super-easy to do. Now I need to consider 2 possibilities.

1) use a one-to-many

  • Pro: Possible the best way to do it in Silverstripe.
  • Con: The content-editor needs to fill an extra form when submitting data

2) use a hard coded str_replace on the $content

  • Pro: The content-editor doesn't need to fill in an extra form when submitting data
  • Con: Not so nice coding, and probably a bit buggy

Anyway, thanks for the suggestion!.