Episerver Search filter using parent page of Page reference type using UnifiedSearchFor - episerver

I have to implement search criteria in episerver. The existing search criteria includes pagetype filter which provides me the search result for the pagetype irrespective of where they are. I need to include another criteria of a parent page which is of type pagereference which basically will filter the pages and return records only which are part of the parent page.
kindly advice.

Well first if you have the parent page they why not use the ContentLoader instead of a search? Like this:
var loader = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentLoader>();
var children = loader.GetChildren<%pageTypeHere%>(%parent pagereference%);
But if you wish to go with the search option:
query = query.Filter(x => x.Ancestors().Match(%parent pagereference id (so a int)%));

Related

CakePHP How to accomplish a NOT IN

I want to accomplish a NOT IN clause in CakePHP, but I am not able to achieve it. I've been reading the CakePHP Cookbook and some answers here in StackOverflow but it's not working for me.
I have a Table named 'Hotel' with all its model, controller and views. In the view template I built a CakePHP Cell where I want to show other Hotels, except for the hotel that is currently being viewed. If for example, I am showing the view of a hotel with id #5, I want to show other hotels options, except for that hotel view id number.
I have the following query in the Cell Controller:
$hotels = $this->Hotels->find('all')
->where(['Hotels.id NOT IN' => $current_hotel_id])
->limit(4)
->order('rand()')
->toArray();
I want $current_hotel_id to have the value of current hotel that it's being viewed. That's the approach I am taking.
Any useful information would be appreciated.
If you need some value that is available in your controller in cell, you must pass that value to it.
First, declare an argument in cell action:
class HotelsCell extends Cell{
public function hotels($current_hotel_id){
//your code here
}
}
Then, in your view, pass argument to cell:
$this->cell("Hotels::hotels",[$hotel->id])
More info in docs: Passing Arguments to a Cell

How to make a search with separate views in Angularjs?

How can I have an index page accept search input and then return the result in different page (view) with angularjs?
All the tutorials I have gone through on the internet shows search box that accepts the query and instantly prints results beneath it on the same page (or say "view" in MVC language?). I want a separate page to just hold the search box, let user type what it wants and then hit go button which then return results on a separate page (which does not have the search box).
You need to store the search box value somewhere before going to the second page. You can do this in several ways.
One way would be to use a service to store the value, then the result page can retrieve it from there.
The service:
.service('CommonService', function(){
this.value = '';
});
The search controller:
.controller('search', function(CommonService){
$scope.goToResult = function(searchBoxValue){
CommonService.value = searchBoxValue;
// Redirect to result view
};
});
This function could be on the ng-submit of the search form.
The result controller:
.controller('result', function(CommonService){
// Receive value of search box
$scope.receivedValue = CommonService.value;
});
Another way would be to send the value along the URL, like GET params:
http://google.com/q?SearchBoxValue=something-i-wrote-before
are you implementing single page application (SPA)? you can do this multiple way with partial view and override partial view on search page with result page.
search page (partial) hit send request to get data.
receive data from service
redirect to result (partial view).
you done.
hope this help you.

Get number of components in placeholder, Sitecore

I have a component that needs to know how many components thats currently added to the very same placeholder, this since it needs to update a value of an html-attribute based on its index within the placeholder.
Is there anyway to get either the number of components thats already added to a placeholder, or to get the current-renderers index?
Usually I would just use a simple for-loop and set the attribute, but since its a placeholder with components thats not an option.
Thanks in advance!
Try this:
var placeholder = "my-placeholder";
var renderingReferences = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);
var renderingsInPlaceholder = renderingReferences.Where(r => r.Placeholder.EndsWith('/' + placeholder, StringComparison.OrdinalIgnoreCase));
var numberOfRenderingsInPlaceholder = renderingsInPlaceholder.Count();
Update: Changed search for placeholder key from IndexOf to EndsWith.
What is the HTML attribute you are trying to update and how are you planning on update this value? From C# code at render time? Or do you want to store this value when a component is added in the Page Editor and store the value against a Sitecore Item?
It depends on what your use-case is, but by suggestion for front-end use would be to execute this logic using JavaScript, otherwise you have to hook into Sitecore pipelines, find your HTML element and the add the attribute appropriately. Saving this value when a component is added means you need to r-run the login for the entire placeholder and update any stored values, since it will (should) be possible for the user to components anywhere...
Something like the following to add a order order data-attribute to a list of elements. If this is being used by another plugin the make sure you run this code before initializing your plugin.
// get element + its siblings
var $els = $('.selector').siblings().addBack();
// loop and add data-attr with index number
$els.each(function(index, element) {
$(this).attr('data-sortorder', index);
}

Contextual Filter Issues with Path Alias vs Content ID

I have a simple content type that has a "parent" field on every content type. This parent field is shown by an entity reference in each of the other associated content types. If there is a parent content type then you can associate these other content types to it.
Using views, I have page that shows all the results of "notes" for example. Notes has a parent associated with it that must be selected when you create a new note. So every note has a parent.
I have been attempting to show all the notes of a certain parent through the URL like so (i have pathauto and alias' setup):
committees/parent/notes
Using contextual filters and entity reference (parent) I am attempting to show these notes based on parent context. I have under relationships: Entity Reference: Parent and under Contextual filters (Content entity referenced from field_parent) Content: Parent. When I preview this in the auto preview area, nothing shows. For example a parent name is "fire" and I enter it in the preview contextual filter and nothing shows.
I have it setup like so:
When the filter value IS NOT in the URL:
Checked Provide Default Value
Type: Raw Value from URL (URL is committees/parent/notes)
Path Component: 2 (parent)
Checked - Use Path Alias
When the filter value IS in the URL or Default Provided:
Checked - Specify Validation Criteria
Selected - Validator: PHP Code
PHP Validate Code:**
$handler->argument = str_replace("-"," ",$argument);
return true;
However using Contextual filters (Content entity referenced from field_parent) Content: Nid and then put in the node id of the parent (fire in this case), I am able to see the notes for "fire" for the certain parent node id (which is the same as fire just the nid). This is exactly what I want.
What am I missing here for the parent to be referenced in the URL and show the notes based on that parent? Why does the NID work and not the PARENT found in the URL?
When referencing drupal 7 uses nid for the reference. In your case you should use nid in the url or finding the nid from the parent alias.
For the latter you can use drupal_get_normal_path() function which returns node/nid path and then extract the nid in your php filter like
$normalpath = drupal_get_normal_path($alias)
$parts=explode('/', $normalpath );
$nid=$parts[1];
Try with this code for
When the filter value IS in the URL or Default Provided:
$np = explode('/', drupal_get_normal_path($argument));
if (!empty($np[1])) {
$handler->argument = $np[1];
return TRUE;
} else {
return FALSE;
}
Check this!

Generate new page from list of articles and navigate - cakephp

I have a list of articles where I can select with a checkbox and I want to generate a page with a list of the selected articles. Then in this new page I want to navigate through those selected articles.
Should I save the ids of the selected articles in session in order to maintain them in the nmew generated page?
Is there a better way?
Thanks in advance
if you want the users to be able to come back to that list later on using url, then you need to put the ids in url. If you don't want them to do that, use Session is fine.
You could post the ID's through a form to a seperate (or the same) method and use the array of ID's in the condition of your $this->Model->find. Could look something like:
function list() {
$conditions = array();
if(isset($this->data)) {
$conditions = $this->data;
}
$this->set('results', $this->Model->find('all', array('conditions' =>$options)));
}

Resources