Drupal 7 - Show hierarchical taxonomy terms in a field of a view - drupal-7

In Drupal 7 I have a categories taxonomy vocabulary with the following structure:
-Cat one
-Cat two
---Sub cat two
-Cat three
I added a node with the term Sub cat two.
I created a view formatted as table, listing node names and its assigned categories.
In this view, the category field only shows Sub cat two as result. As this term is a child of Cat two, I'd like to show something like Cat two > Sub cat two instead of just the child term.
I have no idea on how to achieve this.
Any hints?
Thanks!

That one was not and simple solution. I solved it this way:
I first installed Views PHP module and enabled it;
I created a content view for my content type formatted as a table;
In this view I created a new field of the type 'Global:PHP', and added the following to the "Value Code" textarea:
$n = node_load($data->nid);
$field = field_get_items('node', $n, 'field_tipo');
$parents = taxonomy_get_parents_all($field[0]['tid']);
$output = array();
foreach($parents as $term) {
array_unshift($output, $term->name);
}
return implode(' ยป ', $output);
And that's it!

Related

Modx Resource List as checkbox for users

Please help, I am stuck with Modx Revo tv input type options.
What I want to achieve is have a checkbox type tv, that displays the resources of a particular parent as checkbox items. So when user checks an item or two, they will be outputted as comma separated values.
Than I will put my tv in a getresources call on the template and it will output some information form the checked resources.
So how do I convert resource list into checkbox options?
The documentation on this is very ambiguous.
Accomplishing this requires some work, but it is not very difficult.
First, create a new Template Variable. Name it whatever you want, for example list_children. Then go to Input Options tab and set Input Type to Checkbox and under Input Option Values enter the following:
#eval return $modx->runSnippet('list_children');
Go to the Output Options tab and select Delimiter in the Output Type dropdown. In the Delimiter textbox write a single comma ,. Apply the Template Variable to your Template of choice and save.
New, create a new Snippet. Name this list_children, or whatever you changed the eval expression to call.
In this snippet, fill in the following:
<?php
$c = $modx->newQuery('modResource');
$c->where(array(
'parent' => 2, // Id to fetch children from
'published' => 1, // Remove this line if you also want to include unpublished resources
'deleted' => 0 // Remove this line if you also want resources that are marked for deletion
));
$c->sortby('menuindex', 'ASC');
$collection = $modx->getCollection('modResource', $c);
$output = array();
foreach ($collection as $v) {
$output[] = $v->get('pagetitle') . '==' . $v->get('id');
}
return implode('||', $output);
I found an alternative way.
Make TV with Input Type Checkbox.
Put #SELECT pagetitle, id FROM modx_site_content WHERE parent=123
!Attn. pay attention on modx_site_content, it needs to reflect MySQL db prefix, which in this case is modx, change 123 to respective parent id.
Change TV output type to Delimiter and coma (,) as delimiter.
Set TV access to respective templates.
Now you can select any or many children of a parent resource which will output their ids as TV output. E.g. let's say our parent 123 had children 33, 34 and 35. In Template variable sections of the resource using the template with access to tv, you will find a checkbox list with children titles. Selecting one or more, e.g. 33 and 35 will output "33,35" in the tv used in chunk.
I found the solution in Modx forums. Lost the link unfortunately.

Symfony Form customize array for entity type

In my Symfony2.6.6 project I have a category entity. I don't know how to say that properly but use the doctrine tree extension to create a tree with the category entity like:
Category 1
Child Category 1
Child Category 2
Category 2
Child Category 3
...
Now when creating the form type for creating a new Category I want to customize the array of the parent field.
The code I use
$builder->add('parent', 'entity', array('class' => 'AcmeBlogBundle:Category', 'property' => 'title'));
generates just an array with all the title as expected. But I want the array values to append a '-' for every level of the tree. Category 1 for example would be '- Category 1' and Child Category 1 would be '-- Child Category 1'. How can I do that?
Sorry if this has been asked before, I don't know how to search for that.
Okay, let's say you have property getLevel() which would return your current item's level. Then in your Category entity file, create a simple method, for example getIndentedTitle thar would return your pre-defined category based on its level.
public function getIndentedTitle() {
return sprintf(
'%s %s',
str_repeat('-', $this->getLevel()),
$this->getTitle()
);
}
The following code will add as many "-" symbols as your current category level and will append its title next to it.
Then in your form builder, you have to specify to use that method:
$builder->add('parent', 'entity', array('class' => 'AcmeBlogBundle:Category', 'property' => 'indentedTitle'));
More info on str_repeat.
Hope you got the idea.

How to group backbone collection by model category, then sort each group by model rank

I am following David Sulc's tutorial on Marionette.
http://davidsulc.com/blog/2012/04/15/a-simple-backbone-marionette-tutorial/ and in order to learn extending over it.
Now lets say each cat has a name, rank as well as Category
AngryCat = Backbone.Model.extend({
urlRoot: '/api/cats',
defaults: {
name: "New Cat Name",
category: "Red Cat"
}
});
Now I want to make a composite view like this:
Category 1: Red Cat (03 Cats)
Cat 1
Cat 2
Cat 3
Category 2: Blue Cat (02 Cats)
Cat X
Cat Y
How can I achieve this. Please help!
You can use the groupBy function to group your AngryCat models by their by category property, then use sortBy to sort each group individually by its rank (I am assuming your model has a property named rank that is a number). I also assume you have an intial collection variable named angryCats that contains your models.
var groups = angryCats.groupBy(function(ac) {
return ac.get("category");
});
groups.each(function(group, key) {
// This will output the category name & item count
console.log(key + " " + group.length + " items");
group.sortBy(function (model) {
return model.get("rank");
});
group.each(function(model) {
// This will output the model name
console.log(model.get("name"));
});
});
Additionally, if your wanted to sort each group by a string property of the model, such as the name property, you'll need to create a new locally scoped angryCats collection out of each group (the groups are arrays), then sort each collection by using a comparator as the linked question outlines.
Hope this helps
You can probably achieve what you want to do by combining dcarson's code with my blog post on nested views: http://davidsulc.com/blog/2013/02/03/tutorial-nested-views-using-backbone-marionettes-compositeview/
You need to provide a collection of groups to the composite view. The composite view's item views will be a collection view displaying the cats in that group (i.e. what is done in the linked blog post).

Agile Toolkit manipulating Grid column content

Just started using ATK4 and appreciating it very much so far, but not sure how to do this...
What I am trying to accomplish:
I am outputting a query's results to a grid, one of the fields is 'status', the data will either be '-1' or '1'.
Instead of outputting -1 or 1 to the column, how do I output an HTML snippet (or whatever I need to to get what I want) instead that shows a different icon for each value?
In short:
In column 'status':
if the value is -1, display iconDown.gif;
if the value is 1, display iconUp.gif
Code so far:
class page_showlist extends Page {
function init(){
parent::init();
$q=$this->api->db->dsql();
$q->table('remote_system')
->join('customers.id','customer_id')
->field('customer_id')
->field('ip')
->field('nickname')
->field('name','customers')
->field('status')
;
$grid = $this->add('Grid');
$grid->addColumn('text','status')->makeSortable();
$grid->addColumn('text','name')->makeSortable();
$grid->addColumn('text','ip');
$grid->addColumn('text','nickname');
$grid->addButton('Reload Grid')->js('click',$grid->js()->reload());
$grid->addQuickSearch(array('name'));
$grid->setSource( $q );
}
}
Any pointers/tips?
To add column with icons in Grid you can use custom template.
In one of my projects I do like this:
$url = $this->api->pm->base_url . $this->api->locateURL('template', 'images/');
$grid->addColumn('template', 'type', false)
->setTemplate('<img src="' . $url . 'icon_object_<?$type?>.png">');
It'll use model field named type (in your case use status) and show icons in that column. Icon source URL is generated dynamically and it'll search for image files in your template/images directory named icon_object_XXX.png where XXX value will be taken from field type value.
In my case type is like this: array('building','apartment','land','garage') etc.
And one more thing - you should start using Models whenever possible! That way you'll ease your life later when your project becomes bigger. Also can have extra security (conditions, etc.) with them.

Styling/Theming Drupal 7 Content Type Record

I developed a Content type of "Car Sales" with following fields:
Manufacturer
Model
Make
Fuel Type
Transmission (Manual/Automatic)
Color
Registered? (Yes/No)
Mileage
Engine Power
Condition (New/Reconditioned/Used)
Price
Pictures (Multiple uploads)
I have developed View of this Content Type to display list of cars. Now I want to develop a screen/view for individual Car Sale Record like this:
Apart from arranging fields, please note that I want to embed a Picture Gallery in between. Can this be achieved through Drupal 7 Admin UI or do I need to create custom CSS and template files? If I need to edit certain template files/css, what are those? I'm using Zen Sub Theme.
I would accomplish this by creating a page, and then creating a node template to accompany it. Start by creating a new node, and then record the NID for the name of the template.
Then, in your template, create a new file, and name it in the following manner: node--[node id].tpl.php
Then, in that file, paste in the following helper function (or you can put it in template.php if you're going to use it elsewhere in your site):
/**
* Gets the resulting output of a view as an array of rows,
* each containing the rendered fields of the view
*/
function views_get_rendered_fields($name, $display_id = NULL) {
$args = func_get_args();
array_shift($args); // remove $name
if (count($args)) {
array_shift($args); // remove $display_id
}
$view = views_get_view($name);
if (is_object($view)) {
if (is_array($args)) {
$view->set_arguments($args);
}
if (is_string($display_id)) {
$view->set_display($display_id);
}
else {
$view->init_display();
}
$view->pre_execute();
$view->execute();
$view->render();
//dd($view->style_plugin);
return $view->style_plugin->rendered_fields;
} else {
return array();
}
}
Then add the following code to your template:
<?php
$cars = views_get_rendered_fields('view name', 'default', [...any arguments to be passed to the view]);
foreach ($cars as $car): ?>
<div>Put your mockup in here. It might be helpful to run <?php die('<pre>'.print_r($car, 1).'</pre>'); ?> to see what the $car array looks like.</div>
<?php endforeach;
?>
Just change the placeholders in the code to whatever you want the markup to be, and you should be set!
As I mentioned above, it's always helpful to do <?php die('<pre>'.print_r($car,1).'</pre>'); ?> to have a visual representation of what the array looks like printed.
I use views_get_rendered_fields all the time in my code because it allows me to completely customize the output of the view.
As a Reminder: Always clear your caches every time you create a new template.
Best of luck!

Resources