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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Only show the image from $content


Go to End


3 Posts   1760 Views

Avatar
jeroeng

Community Member, 2 Posts

22 February 2010 at 11:29pm

Edited: 22/02/2010 11:41pm

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
bummzack

Community Member, 904 Posts

23 February 2010 at 1:52am

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

Community Member, 2 Posts

23 February 2010 at 2:03am

Edited: 23/02/2010 2:09am

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!.