Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Remove images from search results

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2
Go to End
Author Topic: 2223 Views
  • Stompit
    Avatar
    Community Member
    17 Posts

    Re: Remove images from search results Link to this post

    Thanks Willr! I no longer get an error message, however the search results still contain images, the code shows the same markup as the original search method.

    Any ideas?

    Thanks
    D

  • Stompit
    Avatar
    Community Member
    17 Posts

    Re: Remove images from search results Link to this post

    Bumpiddy bump bump

  • Stompit
    Avatar
    Community Member
    17 Posts

    Re: Remove images from search results Link to this post

    Hi Willr,
    I've gone through this all with a fine tooth comb, have also double checked to ensure that the live site is working with the updated code. Unfortunately, the search results still include images It would be great if we could nail this one as I have noticed a couple of other similar questions on the forum.

    The problem images are included in the body content thusly:

    <p><img blah blah>blah blah</p>

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Remove images from search results Link to this post

    Oh sorry Stompit I didn't know you were referring to images inside the content. My code will only remove images from being searched as standalone assets. Removing images from posts is another kettle of fish.

    A couple ways you could do it:

    - The easiest way (and what I normally do) is display the results using $Content.NoHTML which will strip all the formatting / images / heading out.

    - If you like formatting then a possible solution is to have a regular expression which replaces the image tags with nothing. I'm not a regular expression expert so would have no clue what the actual code to get that working but how you would do it would be to have a function in your Page class like

    function NoImages() {
    $content = $this->Content;

    return DBField::create('HTMLText', preg_replace("/<img[^>]+\>/i", " ", $content));
    }

  • Stompit
    Avatar
    Community Member
    17 Posts

    Re: Remove images from search results Link to this post

    Thanks again Willr

    The saga continues...

    Referring back to my original code there is no reference to $content, however I have tried to assign the .NoHTML to $form an a few other elements with no luck, keeps throwing errors. I am working with the page.php file.

    Would I find the $Content in another file, or am I missing something obvious!?

    Thanks
    D

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Remove images from search results Link to this post

    Would I find the $Content in another file, or am I missing something obvious!?

    This would be in your search results template. Page_results.ss or similar.

  • Stompit
    Avatar
    Community Member
    17 Posts

    Re: Remove images from search results Link to this post

    Hooraa! Modifying that template worked like a charm!!

    I didn't end up using the .NoHTML, doing so produced a huge chunk of text in the results.

    I had: $Content.FirstParagraph(html) which I changed to $Content.FirstParagraph, seemed to do the job

    Thanks a million for all your help!

    Best Regards
    D

    2223 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.