Drupal Views arguments should show both synonyms and siblings - drupal

I am working with Drupal Views, that filter our content based on geography, and shows news from a specific municipality.
Our taxonomy is hierarchical:
Regions
-- Municipalities from the region
--- Towns from the municipality
For example:
Region A (region)
-- Municipality X (municipality)
--- Town 1
--- Town 2
-- Municipality Y (municipality)
--- Town 3
--- Town 4
-- Municipality Z (municipality)
--- Town 5
--- Town 6
We tag our nodes with town names, and by using taxonomy term id as argument in our view, we can easily list all stories from any municipality.
Now we would like to add a new feature to our view: Also list news from the neighbouring municipality. Municiplaity Y is a Neghbour of Municipality Z, and we added this relation to the taxonomy term.
So now we can show all stories from Y by choosing using Taxonomy Depth argument. We can also show all stories, by using the Taxonomy Related terms argument.
But how to show all nodes from both Y and its sibling Z with views?

You should change the validator of the views argument to PHP code. Then, in the validator field you will be able to change the argument dynamically. This way, having the tid you can get all the related terms.
set the Action to take if argument is not present to "display empty text" ;
on validator options, set the validator to "PHP Code";
in the code:
get the tid using arg(1);
use taxonomy_get_related($tid) to get the related terms;
build a string in with the syntax "tid+related_tid+related_tid+related_tid" to return as argument;
redefine $handler->argument as the built string;
finally, set the Allow multiple terms per argument as True
Here's an example code to insert in the php code validation form:
$tid = arg(1);
$result = strval($tid);
$related = taxonomy_get_related($tid);
foreach($related as $i){
if (intval($i)>0){
$result.="+".$i;
}
}
$handler->argument = $result;
return $result;

Related

How do I access a calculated field in Rails?

In my first attempt to develop something in Ruby on Rails :) ... I have a list of names stored in fields "first_name" and "last_name". In my Person model, I have defined something like this:
def sort_name
sort_name = last_name + ',' + first_name
end
Now I want to show all persons shown in a list, sorted by sort_name, but (in my controller) something like
#persons = Person.order(:sort_name)
doesn't work (Unknown column 'sort_name' in 'order clause'). How do reference to the calculated field sort_name in my controller?
I am sure this is a "oh my god I am so stupid moment" but happy for any advise!
If the model Person has the fields name, first_lastname and second_lastname, you can do the next:
Person.order(:name, :first_lastname, :second_lastname)
By default is ordering in ascending way. Also you can put if you want ascending or descending way for each field:
Person.order(name: :asc, first_lastname: :desc, second_lastname: :asc)
Additional if you want add a column with the complete name, you can use select, using postgresql the code would be:
people = Person.order(
name: :asc, first_lastname: :desc, second_lastname: :asc
).select(
"*, concat(name,' ', first_lastname, ' ',second_lastname) as sort_name"
)
people[0].sort_name
# the sort_name can be for example "Adán Saucedo Salas"

Wordpress custom fields - nesting foreach

I'm a wordpress beginner.
I have some custom fields that contain multiple values, and I want to output them.
One custom field has multiple values (press review quotes for a show - reviewchart-review). But I also want it to output the corresponding value in the reviewchart-publication field (the name of the publication the press quote is from).
This is the code:
$post_meta_array = get_post_meta($post->ID, 'reviewchart-review');
$review_pub = get_post_meta($post->ID, 'reviewchart-publication');
foreach ($post_meta_array as $post_meta) {
echo $review_pub . '<blockquote><p>' . $post_meta . '</p></blockquote>';
}
This is the output:
Array
"Fresh and assured. This Restoration comedy has japes aplenty. Winning performances from Susannah Fielding and Geoffrey Streatfeild."
Array
“Simon Godwin’s production of George Farquhar’s play is one of the most consistently funny productions of a Restoration comedy you’re likely to see.”
Any help appreciated,
Thanks

Getting the level x value of the currentmember in a parent-child hierarchy in MDX

I have an employee parent-child hierarchy in a dimension called Employees which shows the managerial structure of an organisation. This hierarchy is [Employees].[Managerial].
There is another hierarchy that lists all the employees for an organisation. This is a single level hierarchy and it is [Employess].[All Employees].
I have a query that looks something like this:
With
Member measures.[FullTimeSalary] as measures.[Salary] * measures.[FullTimeFactor]
Select {measures.[FullTimeSalary]} on 0,
Non empty
{
[Employess].[All Employees].[All].Children
}
On 1
From MyCube
Where ([Time].[Month].&[201501])
Now if I expand the parent-child hierarchy (the [Employees].[Managerial] hierarchy) I can see each of the different levels of this structure( [Level 02], [Level 03], [Level 04], ect) and what I need to do now is create a new calculated measure called measures.[SupervisingManager] that brings back the currentmembers value at [Level 03] of the hierarchy.
I've tried
member measures.[SupervisingManager] as [Employees].[Managerial].[Level 03].currentmember.member_name
but that just returns "#Error" and using
member measures.[SupervisingManager] as [Employees].[Managerial].currentmember.member_name
returns that currentmember. I also experimented with
measures.[SupervisingManager] as [Employees].[Managerial].currentmember.parent.member_name
but the issue with this is that the currentmember can be located at any within the hierarchy. The only way I can think of doing this is to do a massive case statement, get the ordinal value of the current member and use the appropriate .parent.parent logic. Is there a neater way to do this?
Maybe something along these lines will help:
WITH
MEMBER measures.[FullTimeSalary] AS
measures.[Salary] * measures.[FullTimeFactor]
MEMBER measures.[SupervisingManager] AS
IIF
(
[Employees].CurrentMember.Parent.Level.Name = 'Level 03'
,[Employees].CurrentMember.Parent.Member_Caption
,'n/a'
)
SELECT
{
measures.[FullTimeSalary]
,measures.[SupervisingManager]
} ON 0
,NON EMPTY
{[Employess].[All Employees].[All].Children} ON 1
FROM MyCube
WHERE
[Time].[Month].&[201501];

How to theme an element of an array?

I have this array in a Drupal 7 installation, it outupts the term list that belongs to a specific vocabulary id:
<?php print render($content['taxonomy_vocabulary_3']); ?>
Now, what this does it outputs the result in a list, I would like to output it in a comma separated line.
Now, I suppose that I could do that with a foreach statement?
I´ve tried this, after reading the documentation, but it outputted nothing:
foreach($taxonomy_vocabulary_3 as $id=>$tag) {
echo "$tag, " ;
}
I´ve looked into what the Devel module told me about that array, and it showed me this:
taxonomy_vocabulary_3 (Array, 1 element)
und (Array, 2 elements)
0 (Array, 1 element)
tid (String, 3 characters ) 141
1 (Array, 1 element)
tid (String, 3 characters ) 320
But as you can see it shows the term id in each case, and not the term name...
What do you suggest? Thanks!!
You can load the term and then print it's title.
foreach($vocabulary as $tid) {
$term = taxonomy_term_load($tid);
// print whatever you want from this object.
print $term->title . ', ';
}
taxonomy_term_load
What you got is a build array - so that means that
$content['taxonomy_vocabulary_3']['#theme']
will be the theme function used to render the vocabulary. If you want to change the output you have two good solutions.
override the standard theme function in your theme - this will alter the output of all the calls to that theme function - in this case how all vocabularies is rendered.
Change the #theme value to a theme function of your liking - this could be a custom theme function you define in your theme.
For help on how to render the terms, you can take a look at how the original theme function is implemented - you can look it up at the Drupal API documentation.

Filter a product collection by two categories in Magento

I'm trying to find products that are in two categories.
I've found an example to get products that are in category1 OR category2.
http://www.alphadigital.cl/blog/lang/en-us/magento-filter-by-multiple-categories.html
I need products that are in category1 AND category2.
The example in the blog is:
class ModuleName_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection{
public function addCategoriesFilter($categories){
$alias = 'cat_index';
$categoryCondition = $this->getConnection()->quoteInto(
$alias.'.product_id=e.entity_id AND '.$alias.'.store_id=? AND ',
$this->getStoreId()
);
$categoryCondition.= $alias.'.category_id IN ('.$categories.')';
$this->getSelect()->joinInner(
array($alias => $this->getTable('catalog/category_product_index')),
$categoryCondition,
array('position'=>'position')
);
$this->_categoryIndexJoined = true;
$this->_joinFields['position'] = array('table'=>$alias, 'field'=>'position' );
return $this;
}
}
When I'm using this filter alone it perform OR query on several categories.
When I combine this filter with prepareProductCollection of Mage_Catalog_Model_Layer
it somehow remove the filter effect.
How can I change the filter to AND and combine it with prepareProductCollection?
Thanks
Thanks
This code will allow you to filter by multiple categories but avoid completely killing performance if you had to perform multiple collection loads:
$iNumberFeaturedItems = 4;
$oCurrentCategory = Mage::registry('current_category');
$oFeaturedCategory = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('name','Featured')
->getFirstItem();
$aFeaturedCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'url_key'), 'inner')
->addStoreFilter()
->addCategoryFilter($oFeaturedCategory)
->addCategoryIds();
The first step is to get a collection of products for one category (in this case, a Featured category). Next step is to get the IDs of the products, notice that this does NOT perform a load (ref Mage_Core_Model_Mysql4_Collection_Abstract::getAllIds())
$aFeaturedProdIds = $aFeaturedCollection->getAllIds();
shuffle($aFeaturedProdIds); //randomize the order of the featured products
Then get the IDs for a second category:
$aCurrentCatProdIds = $oCurrentCategory->getProductCollection()->getAllIds();
And intersect the arrays to find product IDs that exist in both categories:
$aMergedProdIds = array_intersect($aFeaturedProdIds,$aCurrentCatProdIds);
For this particular use case, we loop until we have sufficient intersecting products, traversing up the category tree until we find a large enough match (but stopping at root category!):
while(count($aMergedProdIds) < $iNumberFeaturedItems && $oCurrentCategory->getId() != Mage::app()->getStore()->getRootCategoryId()):
$oCurrentCategory = $oCurrentCategory->getParentCategory();
$aParentCatProdIds = $oCurrentCategory->getProductCollection()->getAllIds();
$aMergedProdIds = array_intersect($aFeaturedProdIds,$aParentCatProdIds);
endwhile;
Finally, filter our initial collection by the IDs of the intersecting products, and return.
$aFeaturedItems = $aFeaturedCollection->addIdFilter(array_slice($aMergedProdIds,0,$iNumberFeaturedItems))->getItems();
return $aFeaturedItems;
I am also working on this to no avail, it was available in magento 1.3 using the attribute filter with finset on the category_ids column, however this was moved into the index table from the entity table and now no longer works.
There is one possible solution, but requries an override function which I found here
But this solution is far from ideal.
I think that it will be enough to call addCategoryFilter() twice on your product collection - once for each category. I have not tested it though, so might be wrong.

Resources