Hardcode Custom Template View - Search Block

Permalink 3 users found helpful
Hi All

I have search the forums and their are lots of queries along these lines but none of them has solved my problme.

I would like to hardcode in the Search block using the Coolinput search template.

I have put:

<?php $b = Block::getByName('sitesearch');
   if( is_object($b) ) $b->display(); ?>


I have tried a variety of render view codes but cant get them to work, and most use the getbyhandle command which I don't really get . (not really hot at this php malarky.

I have added the coolinput content to my blocks folder and I have selected it in the site scrapbook where my sitesearch block is set up.

Can anyone advise me as top what I need to put to get the correct custom template?

Thanks in advance.

 
ijessup replied on at Permalink Reply
ijessup
When I make a block I usually include a preview mode. Here is an example:
<?php defined('C5_EXECUTE') or die("Access Denied.");
if($_REQUEST['use_block_template']) {
   $view = 'templates/'.$_REQUEST['template'];
} else {
   $view = 'view';
}
$bt = BlockType::getByHandle('virtual_tables_list');
$bt->controller->itemsPerPage   = $_REQUEST['itemsPerPage'];
$bt->controller->sortBy         = $_REQUEST['sortBy'];
$bt->controller->sortByDir      = $_REQUEST['sortByDir'];
$bt->render($view);
?>
Note the render() function. For a template it would be:
$bt->render('templates/my_template');
zoinks replied on at Permalink Reply 1 Attachment
How would a hard-coded search block work? Don't you need to click the search while in Edit mode to specify which pages you want searched and if you want the search-results posted below or on another page?

IE, this stuff below? (or see screenshot attachment)

Search Title:
Submit Button Text:
Search Within Path:
everywhere
beneath this page
beneath another page
Results Page:
Post to Another Page Elsewhere
search/search-results
zurcxer replied on at Permalink Reply
zurcxer
Please see jordanlev solution below.
zurcxer replied on at Permalink Reply
zurcxer
Please see jordanlev solution below.
jordanlev replied on at Permalink Reply
jordanlev
You actually don't even need to hard-code a search block on your pages. What I always do is create one page in my site for search results (let's say it's at the addresshttp://example.com/search-results),... I put one search block in the main content area on that page, then in my theme templates I just put a straight-up html form for the search box. The only requirements for this "manual" search form is that the "action" goes to the search results page you created, and that there's a text input field whose name is "query". For example:
<form class="mysearchform" method="get" action="<?php echo View::url('/search-results'); ?>">
   <input type="text" name="query" class="whatever" />
   <input type="submit" value="Go" class="something" />
</form>
wormracer08 replied on at Permalink Reply
wormracer08
Once again perfect solution Jordan, Thanks!
zurcxer replied on at Permalink Reply
zurcxer
Thanks jordan... That's the perfect solution..
zoinks replied on at Permalink Reply
This doesn't seem to be working.

If I click a Tag in my blog, it shows up on the Search-Results page.

So... if I search for that same Tag in my Search box (your above code), it should find the same thing, shouldn't it? Instead, the Search-Results pages shows 0 items found if I Search.

EDIT: yes, it works but for some reason won't search my Tags. If I click a tag link it shows up on the search-results page, so I don't know what the hell is going on but Search box don't like to search for tags, I guess. In Sitemap > Page Search > Setup Index I selected EVERYTHING and whitelisted it. Tags are not included in this, I guess.
jordanlev replied on at Permalink Reply
jordanlev
Yeah, there's something really weird about the way tags are implemented -- if you inspect the request that's made when clicking on a tag, you'll see it has some weird brackets around a weirdly-named attribute. So just typing in the tag name in the search box doesn't bring it up. I have no idea how to address this issue (but if someone else does I'd be interested in knowing).
zoinks replied on at Permalink Reply
Jordan, I'm glad you replied because now I don't feel as bad for not grasping what was going on there exactly. I looked at those brackets, too and figured it was an internal kind of set-up but that there was some way I could probably extract that information. Then, I just started to believe it was perhaps something incorporated in the Search Block I had to figure out. All in all, I spent quite a bit of time devoted to this mystery for 2 days.
RadiantWeb replied on at Permalink Reply
RadiantWeb
the reason for this is because the search block, by default searches the collectionsearchindexattributes table... as such, tags are actually an attribute. an attribute that has a key name. to be more specific, "ak_tags".

So..if you want to search tags, you would search ?akID[11][atSelectOptionID][]=8. 8 being the atSelectOptions ID for that word.

So in order to search tags by word, you would first (withing the search controller, just inside the "do_search" function) go see if there is an ID for that word.

$keyword = $_REQUEST['query'];
$db = Loader::db();
Loader::model('attribute/categories/collection');
$ak = CollectionAttributeKey::getByHandle('tags');
$wordID = $db->getOne("SELECT ID FROM atSelectOptions WHERE value = ?",array($keyword));
if($wordID){
  $_REQUEST['akID'][$ak->akID] = $wordID;
}


now this would obviously limit you to prefer tag results over any common search result. but I'm sure there are ways around that..or that might even be what you want? dunno.

I'm sure Jordan can expound on this.

ChadStrat
RadiantWeb replied on at Permalink Reply
RadiantWeb
just tested this...had a few things wrong there..but just corrected them and this seems to work just fine.

Chad
zoinks replied on at Permalink Reply
This is extremely helpful to me even just as far as learning how PHP works. Thank you!

Do you know of a way to search keywords in addition to regular search options? Or does it look like an either/or thing?
jordanlev replied on at Permalink Reply
jordanlev
Actually I can't expound on this -- Chad's answer is enlightening to me. Thanks for sharing this very useful code and explanation!
fastcrash replied on at Permalink Reply
fastcrash
My foot print come here too, already looking an hour to search this matter.
i try script that chad providing but no result, or maybe i dont now how to use that.. this how i set them, but no result. i create atributte real_name and want search result all page created by that name
here my code
$_REQUEST['query'] = 'Frente';
      $keyword = $_REQUEST['query'];
      $db = Loader::db();
      Loader::model('attribute/categories/collection');
      $ak = CollectionAttributeKey::getByHandle('real_name');
      $wordID = $db->getOne("SELECT ID FROM atSelectOptions WHERE value = ?",array($keyword));
      if($wordID){
        $_REQUEST['akID'][$ak->akID] = $wordID;
      }

so how can i get result by real_name attribut?
keeasti replied on at Permalink Reply
keeasti
Smooth solution!
Is there a way to restrict where it searches using this method ie. under a specific 'sub-folder' like you can choose when you add the search block at all?
TIA
jordanlev replied on at Permalink Reply
jordanlev
Add a search block to a test page and put in the settings you want for restricting it. Then save the page and inspect the search form with firebug or web inspector -- or just submit a search and look at the querystring -- you should see some kind of parameters it uses to restrict the search. Make those parameters a hidden field in your form and it should work (then you can delete that test block on the test page).
delorentis replied on at Permalink Reply
Hi, I'm trying to hardcore the search block. It worked fine with the answer of zurcxer. My problem is that I want that the search, search beneath a particular page (path or page id). I tried giving a "baseSearchPath" but it didn't work. Any ideas how I can make this work?

<?php
$search = BlockType::getByHandle('search');
$search->controller->title = '';
$search->controller->buttonText = '';
$search->controller->baseSearchPathEverywhere = false;
$search->controller->baseSearchPath = 'fr/';
$search->controller->externalTarget = true;
$search->controller->resultsURL = 'fr/search-fr';
$search->render('templates/customin/view');
?>

thank you for help
jordanlev replied on at Permalink Reply
jordanlev
In Concrete5, paths usually start with a slash and do not end in one, so change "fr/" to "/fr" and see if that works.
delorentis replied on at Permalink Reply
thank you for your answer, Unfortunately it doesn't work. I have tried almost everything:
'/fr' or 'index.php/fr' or 'full path/fr' and so on..
with the variants above I have no search result at all, and if I try without writing anything at all like:
$search->controller->baseSearchPath = '';
The search block search beneath every page.

maybe ->baseSearchPath isn't the correct attribute to allow the search blocks to search beneath a particular page?
Mnkras replied on at Permalink Reply
Mnkras
This may just be me personally, but the search block is SUPER simple, just place the html, less overhead, hardcode it.

<form action="<?php echo View::url('/fr/search-fr');?>" method="get">
<input name="query" type="text"/>
<input name="search_paths[]" type="hidden" value="/fr"/>
<input name="submit" type="submit" value="Search">
</form>


you can customize it however you want.

Mike
delorentis replied on at Permalink Reply
THANK YOU it worked perfectly ! Now I can search beneath the french or english side of my website.
I have one last question:
how can I do to have the results of research on the same page and not
on another page url (see attachments) ?

Best
jordanlev replied on at Permalink Reply
jordanlev
This is confusing because the search block serves 2 purposes:
1) Search form
2) Search results

You do not need to use the search box for purpose 1 -- you can use some HTML like @Mnkras posted above. BUT for purpose 2, you must use the search block.

If you want the search box to post to the same page it's already on, you need to use the search block (not the hardcoded html that @Mnkras posted). If you want the hardcoded html form on all pages *except* the search results page (which has the actual search block on it), you can put some code like this around the hardcoded html form:
<?php if ($c->getCollectionPath() != '/fr/search'): ?>
<form action="...>
 etc. etc.
</form>
<?php endif; ?>


You will need to replace the '/fr/search' path with whatever the path to your actual search page is.
delorentis replied on at Permalink Reply
Thank you for the quick response!
It works fine, Now everything is just perfect!
Once again thank you for your help!

Best


(I'm creating my fourth website with concrete5 and I use a responsive layout with media queries and the research block is in different places if you're on desktop mode or mobile mode. So I had to hardcode the search block for the mobile mode to display it on a different place..)
keeasti replied on at Permalink Reply
keeasti
Thanks Jordan ... that worked
Thought I'd post how I did it in case anyone else needs it.
I added a hidden field like so:

<input name="search_paths[]" type="hidden" value="/page-name-to-search-under" />


The whole snippet looks a bit like this:

<form method="get" action="<?php echo View::url('/name-of-your-search-result-page'); ?>">
     <input name="search_paths[]" type="hidden" value="/page-name-to-search-under" />
     <input type="text" onFocus="if(this.value=='Search...') this.value='';" onBlur="if(this.value=='') this.value='Search...';" class="searchBoxSide" value="Search..." name="query">
     <input type="submit" class="searchButton" value="Go" name="submit">
    </form>


The inline JS just makes the Search... appear and disappear when you click into the box.
graphiczweb replied on at Permalink Reply
graphiczweb
Super easy! Thank you so much!