tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/14640-query-using-selected-option-value-from-selectCascade CMS: Discussion 2016-10-02T02:45:15Ztag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-18T18:30:04Z2016-08-18T18:40:17ZQuery using selected option value from select<div><p>Hi Steve,</p>
<p>I believe that you need to use <code>hasMetadata()</code> to
filter out unwanted pages. <code>hasMetadata()</code> takes two
String parameters, the first one being the identifier of the field
(could be a dynamic field), the second being a value you want the
field to have.</p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T13:16:37Z2016-08-19T13:57:41ZQuery using selected option value from select<div><p>Thanks Wing. I don't know how I could set the second argument of
the hasMetadata() to the selected option from the select prior to
the actual query. There is always going to be a scope
issue.<br></p>
<pre>
<code>#set ( $query = $_.query() )
#set ( $query = $query.byMetadataSet("Main-Taxonomy-List") )
#set ( $query = $query.byContentType("news-detail") )
#set ( $query = $query.includePages(true) )
#set ( $query = $query.includeFiles(false) )
#set ( $query = $query.includeBlocks(false) )
#set ( $query = $query.includeFolders(false) )
#set ( $query = $query.includeSymlinks(false) )
#set ( $query = $query.sortBy("created") )
#set ( $query = $query.sortDirection("desc") )
#set ( $query = $query.maxResults(12) )
#set ( $news = $query.execute() )
<div class="region news-list-region">
<div class="region-holder">
<!-- Filter form -->
<form class="filter-form">
<div class="filter-form-holder">
<div class="filter-form-item">
<span class="filter-form-label">Search by:</span>
</div>
<div class="filter-form-item">
<div class="filter-form-select-wrapper">
<select class="filter-form-select" name="" id="select-1">
<option value="Recent news">Recent news</option>
<option value="2015-2016">2015-16</option>
<option value="2014-2015">2014-15</option>
<option value="2013-2014">2013-14</option>
</select>
</div>
</div>
<div class="filter-form-item">
<div class="filter-form-select-wrapper">
<label for="topics" class="hide-for-screen-reader">Please choose topic</label>
<select class="filter-form-select" name="topics" id="topics">
<option value="All Topics">All Topics</option>
#foreach( $news_items in $news_list_page_metadata )
#if( 'news-and-events' == $news_items.getChild('name').value )
#set( $taxonomy_list = $news_items.getChildren('value') )
#foreach( $taxonomy_item in $taxonomy_list )
<option value="${_EscapeTool.xml($taxonomy_item.value)}">${_EscapeTool.xml($taxonomy_item.value)}</option>
#end
#end
#end
</select>
</div>
</div>
<div class="filter-form-item">
<button type="submit" class="filter-form-btn btn btn-primary">Search</button>
</div>
</div>
</form>
<!-- News list -->
<ul class="news-list">
#foreach( $single_news in $news.hasMetadata("news-and-events", "${_EscapeTool.xml($taxonomy_item.value)}") ))
#set( $headline = $single_news.getStructuredDataNode('headline').textValue )
#set( $byline = $single_news.getStructuredDataNode('byline').textValue )
#set( $calendar = $single_news.getStructuredDataNode('calendar').textValue )
#set( $thumbnail_image = $single_news.getStructuredDataNode('thumbnail-image'))
#set( $news_link = $single_news.link )
#set( $metadata = $single_news.metadata )
#set( $dynamic_fields = $metadata.dynamicFields )
<li class="news-list-item">
<article class="news-block is-blue">
<div class="news-block-holder">
#set( $stop_loop = false )
#if( $dynamic_fields.size() > 0 )
#foreach( $dynamic_field in $dynamic_fields )
#set( $chanwTextSize = $dynamic_field.Values.size() )
#if( $chanwTextSize > 0 )
#foreach( $value in $dynamic_field.Values )
#if( $value != "" )
<strong class="news-block-taxonomy">$value</strong>
#set( $stop_loop = true )
#break
#end
#end
#end
#if( $stop_loop )
#break
#end
#end
#end
<div class="news-block-img">
#if (!$_PropertyTool.isNull($thumbnail_image.asset))
<img src="${thumbnail_image.asset.link}" alt="${thumbnail_image.asset.metadata.title}"/>
#else
#set( $thumbnail_image = '/assets/images/img-news-list-01.jpg' )
<img src="$thumbnail_image" alt="News Image" />
#end
</div>
<div class="news-block-body">
<a href="${news_link}" class="news-block-link">$headline</a>
</div>
</div>
</article>
</li>
#end</code>
</pre></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T14:17:51Z2016-08-19T14:25:46ZQuery using selected option value from select<div><p>Steve,</p>
<p><code>hasMetadata</code> is a method of the <code>$query</code>
object and has to be called right at the beginning. This means that
you will only get pages whose metadata field of that id has that
value. This is a very severe restriction imposed on the
<code>$query</code> object by Cascade, and I don't think you can
retrieve pages that do not have that field or pages having that
field but not that value.</p>
<p>See <a href="http://www.hannonhill.com/kb/Script-Formats/">http://www.hannonhill.com/kb/Script-Formats/</a>
for more discussion and <a href="http://www.upstate.edu/cascade-admin/formats/velocity/api-documentation/com-hannonhill-cascade-velocity/api-locator-tool-search-query.php">
http://www.upstate.edu/cascade-admin/formats/velocity/api-documenta...</a>
for the API documentation.</p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T14:26:56Z2016-08-19T14:26:56ZQuery using selected option value from select<div><p>Rats! Could I instead filter the results after the query is
executed at this point? (where $news is the query execute()
)<br></p>
<pre>
<code> #foreach( $single_news in $news )
#set( $headline = $single_news.getStructuredDataNode('headline').textValue )
#set( $byline = $single_news.getStructuredDataNode('byline').textValue )
#set( $calendar = $single_news.getStructuredDataNode('calendar').textValue )
#set( $thumbnail_image = $single_news.getStructuredDataNode('thumbnail-image'))
#set( $news_link = $single_news.link )
#set( $metadata = $single_news.metadata )
#set( $dynamic_fields = $metadata.dynamicFields )
<li class="news-list-item">
<article class="news-block is-blue">
<div class="news-block-holder">
#set( $stop_loop = false )
#if( $dynamic_fields.size() > 0 )
#foreach( $dynamic_field in $dynamic_fields )
#set( $chanwTextSize = $dynamic_field.Values.size() )
#if( $chanwTextSize > 0 )
#foreach( $value in $dynamic_field.Values )
#if( $value != "" )
<strong class="news-block-taxonomy">$value</strong>
#set( $stop_loop = true )
#break
#end
#end
#end
#if( $stop_loop )
#break
#end
#end
#end</code>
</pre></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T14:30:34Z2016-08-19T14:30:34ZQuery using selected option value from select<div><p>Sure. But then you have to work with the
<code>MetadataAPIAdapter</code> objects and
<code>DynamicMetadataFieldImpl</code> objects. See my API
documentation for available methods in these two classes.</p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T14:33:09Z2016-08-19T14:33:09ZQuery using selected option value from select<div><p>Thanks Wing! Every time I try to access your documentation it
says I need a login to access the page. Do you have a new URL?</p></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T14:36:11Z2016-08-19T14:37:52ZQuery using selected option value from select<div><p>Make sure the domain in the URL starts with <a href="http://www.upstate.edu">www.upstate.edu</a>. Sometimes I
inadvertently copied a URL from our development server, starting
with web.upstate.edu.</p>
<p><a href="http://www.upstate.edu/cascade-admin/formats/velocity/api-documentation/index.php">
http://www.upstate.edu/cascade-admin/formats/velocity/api-documenta...</a></p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T15:18:31Z2016-08-19T15:18:31ZQuery using selected option value from select<div><p>Okay, how do I instantiate either of the API adapter objects in
Velocity?<br>
(didn't like this)</p>
<pre>
<code>#set ( $metaDataField = DynamicMetadataFieldImpl() )</code>
</pre>
Then I tried to use a method:<br>
<pre>
<code>$metaDataField.getValue('news-and-events')</code>
</pre></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T16:06:13Z2016-08-19T16:06:13ZQuery using selected option value from select<div><p>Cascade API objects are created by Cascade and supplied to you.
You never instantiate them. When you start with
<code>$currentPage</code>, for example, call
<code>getMetadata</code> method through the object, what you get is
a <code>MetadataAPIAdapter</code> object. And when you call
<code>getDynamicField</code> through the
<code>MetadataAPIAdapter</code> object, you get a
<code>DynamicMetadataFieldImpl</code> object. So you have to go
through these objects and call the right methods to get to the
values. My API documentation becomes handy when you need to do
that.</p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T16:08:48Z2016-08-19T16:09:53ZQuery using selected option value from select<div><p>Sounds like reflection?<br>
Then why did I have to create an instance of the query object
using:<br></p>
<pre>
<code>$query = $_.query()</code>
</pre></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T16:19:21Z2016-08-19T16:19:21ZQuery using selected option value from select<div><p>That's how Hannon Hill decided to expose the object.
<code>query</code> is probably a factory method returning a
singleton.</p>
<p>Talking about reflection, I introduced it into the discussion of
Velocity last year and developed an entire library of Java objects.
To find out more about this topic, see</p>
<p><a href="https://github.com/wingmingchan/velocity/blob/master/library/chanw_object_creator.vm">
https://github.com/wingmingchan/velocity/blob/master/library/chanw_...</a><br>
<a href="https://github.com/wingmingchan/velocity/blob/master/library/chanw_global_utility_objects.vm">
https://github.com/wingmingchan/velocity/blob/master/library/chanw_...</a></p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T16:20:52Z2016-08-19T16:21:11ZQuery using selected option value from select<div><p>The query object provides <a href="https://en.wikipedia.org/wiki/Method_chaining">method chaining</a>
for things like: <code>hasMetadata</code>,
<code>byContentType</code> where each method returns a version of
the query object with additional criteria or sorting applied.</p></div>Bradley Wagnertag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T16:24:16Z2016-08-19T16:24:16ZQuery using selected option value from select<div><p>Bradley,</p>
<p>Thanks for the info. The chained methods can alter the state of
the query object, so that it can still be a singleton. Right?</p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T16:26:11Z2016-08-19T16:26:11ZQuery using selected option value from select<div><p>It's not specifically implemented as a singleton, no. I would
think about it more as factory method where the resultant object
supports method chaining.</p></div>Bradley Wagnertag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T16:32:03Z2016-08-19T16:32:58ZQuery using selected option value from select<div><p>That's precisely what I am trying to get at. Does it mean that
if I call <code>query()</code> twice, I get two separate instances?
Can I retrieve two separate sets of assets independently?</p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T16:49:22Z2016-08-19T16:49:22ZQuery using selected option value from select<div><p>Correct, multiple <code>query()</code> calls will create
separate query objects.</p></div>Bradley Wagnertag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T16:58:37Z2016-08-19T16:58:37ZQuery using selected option value from select<div><p>Bradley, Is there anyway to tie the query object hasMetadata()
method to a value of a select option?</p></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T17:02:17Z2016-08-19T17:02:17ZQuery using selected option value from select<div><p>Not sure I'm following. Is <code>hasMetadata</code> not working
for you when querying for a specific value?</p></div>Bradley Wagnertag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T18:36:18Z2016-08-19T18:36:18ZQuery using selected option value from select<div><p>I don't know how to get the value of the select into the
hasMetada property since it has to be set at the beginning and the
select with the dropdown creating the option value (that needs to
be in the hasMetadata method) in a separate prior foreach loop as
the query is called, so the variable is out of scope.</p></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T18:46:20Z2016-08-19T18:46:20ZQuery using selected option value from select<div><p>I'm not quite following what this code is trying to do but one
thing I can say is that calling <code>hasMetadata()</code> after
the query has been <code>executed</code> will not return results,
merely a modified version of the query object. it needs to be set
<em>before</em> executing the query and getting the results.</p>
<p>When you're calling:<br></p>
<pre>
<code> #foreach( $single_news in $news.hasMetadata("news-and-events", "${_EscapeTool.xml($taxonomy_item.value)}") ))</code>
</pre>
where are you trying to pull <code>$taxonomy_item</code> from?
<p>If you're able to get that variable to update, thix might be as
simple as calling:<br></p>
<pre>
<code>$newsQuery.hasMetadata(("news-and-events", "${_EscapeTool.xml($taxonomy_item.value)}") )
#foreach ($single_news in $newsQuery.execute())
...</code>
</pre>
<p>Let me know if that makes sense.</p></div>Bradley Wagnertag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-19T19:10:52Z2016-08-22T13:02:29ZQuery using selected option value from select<div><p>Yes, that makes perfect sense, but unfortunately the variable
<code>$taxonomy_item</code> is created within a separate foreach
loop prior. I've initialized the <code>$taxonomy_item</code>
globally, but it doesn't seem to update:<br></p>
<pre>
<code>#set ( $taxonomy_item = "" )
<label for="topics" class="hide-for-screen-reader">Please choose topic</label>
<select class="filter-form-select" name="topics" id="topics">
<option value="All Topics">All Topics</option>
#foreach( $news_items in $news_list_page_metadata )
#if( 'news-and-events' == $news_items.getChild('name').value )
#set( $taxonomy_list = $news_items.getChildren('value') )
#foreach( $taxonomy_item in $taxonomy_list )
<option value="${_EscapeTool.xml($taxonomy_item.value)}">${_EscapeTool.xml($taxonomy_item.value)}</option>
#end
#end
#end
</select>
</div>
</div>
<div class="filter-form-item">
<button type="submit" class="filter-form-btn btn btn-primary">Search</button>
</div>
</div>
</form>
<ul class="news-list">
#set( $newsQuery = $query.hasMetadata("news-and-events", "${_EscapeTool.xml($taxonomy_item.value)}" ))
#foreach ($single_news in $newsQuery.execute())
#set( $metaValue = $metadataField.getValue('news-and-events') )
#set( $headline = $single_news.getStructuredDataNode('headline').textValue )
#set( $byline = $single_news.getStructuredDataNode('byline').textValue )
#set( $calendar = $single_news.getStructuredDataNode('calendar').textValue )
#set( $thumbnail_image = $single_news.getStructuredDataNode('thumbnail-image'))
#set( $news_link = $single_news.link )
#set( $metadata = $single_news.metadata )
#set( $dynamic_fields = $metadata.dynamicFields )</code>
</pre></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-23T15:30:01Z2016-08-23T15:30:01ZQuery using selected option value from select<div><p>Any ideas as to why my variable $taxonomy_item is not
updating?</p></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-25T13:46:47Z2016-08-25T13:46:47ZQuery using selected option value from select<div><p>Hi Voltmer,</p>
<p>I was looking at your code and it appears to me that
$taxonomy_item will only live in your foreach. The moment you exit
that, the variable is no longer available.</p>
<p>I'm a little confused on what you are attempting in velocity.
Are you trying to have Velocity deliver results of a filter/search
provided to web visitors? If so, that isn't possible with this
approach. Velocity will essentially provide static HTML on page
render.</p>
<p>If I am misunderstanding what you are doing above and you are
wanting to create say static lists provided selections of filters
on the Cascade CMS page, I have done something similar for Carnegie
Mellon using a hash map. The following is a snippet of my code from
that project.</p>
<pre>
<code>#set ( $categories = $_XPathTool.selectNodes($newsSettings, "categories/value") )
#set($allArticles = {})
#foreach($category in $categories)
##set( $imagePath = $_EscapeTool.xml($articleObj.getStructuredDataNode("syndication").getChild("image").asset.link) )
#set ( $articles = $_.query().byMetadataSet("News").siteName($currentPageSiteName).hasMetadata("categories", "${category.value}").execute() )
#foreach($article in $articles)
#set ( $articleData = {
"id": $article.identifier,
"title": $_EscapeTool.xml($article.metadata.title),
"link": $_EscapeTool.xml($article.link),
"path": $_EscapeTool.xml($article.path)
})
#set ( $_void = $allArticles.put($articleData.get("link"), $articleData))
#end
#end</code>
</pre></div>Pennytag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-25T13:55:44Z2016-08-25T13:55:44ZQuery using selected option value from select<div><p>Thanks Penny.<br>
I am trying to filter news articles based on the option selected
from a couple of dropdown select fields, which contain metadata
(taxonomy) values to be matched with the news articles with
matching metadata, then displaying them on the page.</p></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-25T14:23:20Z2016-08-25T14:23:20ZQuery using selected option value from select<div><p>Sorry to beat a dead horse but I still need a tiny bit of
clarification, who will be making the "selection" for
filtering?</p>
<p>The web visitor or the person creating the web page in the
CMS?</p>
<p>Or is it a combo? I create a web page and say these are the
categories that my web visitors can filter by?</p>
<p>Thanks!</p></div>Pennytag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-25T14:56:20Z2016-08-25T14:56:20ZQuery using selected option value from select<div><p>Sorry, should have been clearer. The web user will make the
selections from the webpage to view the results.</p></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-25T15:29:15Z2016-08-25T15:29:15ZQuery using selected option value from select<div><p>That was what I thought. So what you are trying to do is partly
not how the system works.</p>
<p>You can create the filter options using Velocity but for the
display of results and on-demand filtering, you can not use
Velocity. Once the page is rendered, Velocity no longer exists.</p>
<p>What I would recommend doing is using Velocity to create a JSON
or XML file of results that can be queried using JQuery or some
server side language like php or .net.</p>
<p>Velocity could be used if you were creating a page with all of
the data preloaded and then using the select to filter down based
on a provided class. Below is an example. But you will notice that
the data is on the page and it is really Javascript that does the
filtering.<br>
<a href="http://www.northeastern.edu/law/news/multimedia/index.html">http://www.northeastern.edu/law/news/multimedia/index.html</a></p></div>Pennytag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-25T15:38:47Z2016-08-25T15:38:47ZQuery using selected option value from select<div><p>Your example is exactly what I would like to achieve. The
default option on the dropdown on page load is "all categories",
then the user would filter it down, by selecting from the
dropdowns.<br>
So, you are just using Velocity $_.query to pull in all the data
into the page on load, then filtering? Do you have an example of
your script?</p></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-25T17:40:17Z2016-08-25T17:40:17ZQuery using selected option value from select<div><p>So all you really need to do with Velocity is pull in all of the
news article data. You would then output each item as an li element
or div or whatever your Jquery/server side script will look for. On
the class, you can then output information for filter classes.</p>
<p>Your Velocity therefore doesn't need to look for hasMetadata. It
just needs to query by Content Type. Get ALL news articles. Then
output all of the necessary data for each article. So it sounds
like you just need one query in Velocity.</p>
<p>You then need to have a Jquery/server side script to do the
filtering. From that example, this is the script they have:
<a href="http://www.northeastern.edu/law/files/scripts/filter-media.js">
http://www.northeastern.edu/law/files/scripts/filter-media.js</a></p></div>Pennytag:help-archives.hannonhill.com,2010-02-09:Comment/405667192016-08-29T19:31:54Z2016-08-29T19:32:54ZQuery using selected option value from select<div><p>Thanks Penny! I've got a problem though. I have many selections
that can be made in each news article's dynamic data, so I am
iterating through them to get all the different selections, but
since this is within a separate foreach I can't pass the metadata
back out to the inclosing list item to have the filter work
correctly (hide/show based on the class named with the pulled
metadata).<br></p>
<pre>
<code> <ul class="news-list news-articles mediaListing">
#foreach ($single_news in $news)
#set( $metaValue = $metadataField.getValue('news-and-events') )
#set( $headline = $single_news.getStructuredDataNode('headline').textValue )
#set( $byline = $single_news.getStructuredDataNode('byline').textValue )
#set( $calendar = $single_news.getStructuredDataNode('calendar').textValue )
#set( $thumbnail_image = $single_news.getStructuredDataNode('thumbnail-image') )
#set( $news_link = $single_news.link )
#set( $newsYear = $single_news.getParent().name)
#set( $metadata = $single_news.metadata )
#set( $dynamic_fields = $metadata.dynamicFields )
<li class="news-list-item mediaListingEntry $newsYear $metadata.dynamicFields.value">
<article class="news-block is-blue">
<div class="news-block-holder">
#set( $stop_loop = false )
#if( $dynamic_fields.size() > 0 )
#foreach( $dynamic_field in $dynamic_fields )
#set( $chanwTextSize = $dynamic_field.Values.size() )
#if( $chanwTextSize > 0 )
#foreach( $value in $dynamic_field.Values )
#if( $value != "" )
<strong class="news-block-taxonomy $value">$value</strong>
#set( $stop_loop = true )
#break
#end
#end
#end
#if( $stop_loop )
#break
#end
#end
#end
<div class="news-block-img">
#if (!$_PropertyTool.isNull($thumbnail_image.asset))
<img src="${thumbnail_image.asset.link}" alt="${thumbnail_image.asset.metadata.title}"/>
#else
#set( $thumbnail_image = '/assets/images/img-news-list-01.jpg' )
<img src="$thumbnail_image" alt="News Image" />
#end
</div>
<div class="news-block-body">
<a href="${news_link}" class="news-block-link">$headline</a>
</div>
</div>
</article>
</li>
#end
</ul></code>
</pre>
I am also having an issue getting this javascript to load using
require.js</div>voltmer1