How do I add taxonomy terms using Drupal Migrate - drupal

I'm using the migrate module to copy data from several sources to a new drupal installation. So far, I'm able to replicate a lot of what I need from the examples provided with the module. I'm currently stuck on adding terms or taxonomy to newly created nodes. The example shows:
// These are related terms, which by default will be looked up by name
$this->addFieldMapping('migrate_example_beer_styles', 'terms')
->separator(',');
I've tracked down the migrate_example_beer_styles destination mapping and it seems to be the machine name for that taxonomy.
I've tried imitating this behavior with every variation of what my machine_name should be, but the terms never seem to get associated:
By id:
// where source breed_id is '1,100' - it finds mapped values accordingly
$this->addFieldMapping('breeds', 'breed_id')
->sourceMigration('BreedMigration')
->separator(',')
And, by name:
// where source breeds is 'Dogs,German Shepherd'
$this->addFieldMapping('breeds', 'breeds')
->separator(',');
Am I wrong assuming the destination mapping is the machine name for a taxonomy?
This version of the migrate module was released recently, I haven't found any other helpful examples on the web.

This question still seems to be getting some views, so I thought I'd add what else I've discovered. While the accepted answer works, you are able to map Vocabs on ID:
$this->addFieldMapping('Exact Case Sensitive Vocab Name', 'source_field_name')
->sourceMigration('ExactSourceClassName')
->arguments(array('source_type' => 'tid'))
->separator(',');
->separator(',') used for passing a delimited string of source ids. Obviously, leave that off if you're mapping an array of values.

I'm currently working with migrate module myself, and I agree that the documentation is somewhat wanting at this point. :)
The 'machine name' of a vocabulary is listed in the Vocabulary table, in the field 'module'. Try using that value. Do note that you need to feed the text into the mapping, not the ids.

This is my first post on stackoverflow, so I apologize in advance if this isn't the accepted way to submit more information concerning this issue...
I've been stumbling around with the Migrate module for the past few days and was looking for a way to do this in Drupal 7. I had a comma-delimited list of taxonomy ids within an XML field that I wanted to use, but every example I found was retrieving from an external class, or from a database source.
Anyway, through trial and error, I found that you can use a field within the migrate class, rather than reference an external term migration class.
$this->addFieldMapping('field_article_type', 'category_id')
->arguments(array('source_type' => 'tid'))
->xpath('/article/category_id')
->separator(',');

Check out the taxonomy csv import module at http://drupal.org/project/taxonomy_csv.
It was easy to use and did what it was supposed to and more.
I ended up only using the migrate module for importin gNodes and used this module for the taxonomy. It was a pleasure to use.

Related

How to OR solr term facets via the search URL in Drupal 7 site?

I have a Drupal 7 website that is running apachesolr search and is using faceting through the facetapi module.
When I use the facets to narrow my searches, everything works perfectly and I can see the filters being added to the search URL, so I can copy them as links (ready-made narrowed searches) elsewhere on the site.
Here is an example of how the apachesolr URL looks after I select several facets/filters:
search_url/search_keyword?f[0]=im_field_tag_term1%3A1&f[1]=im_field_tag_term2%3A100
Where the 'search_keyword' portion is the text I'm searching for and the '%3A' is just the url encoded ':' (colon).
Knowing this format, I can create any number of ready-made searches by creating the correct format for the URL. Perfect!
However, these filters are always ANDed, the same way they are when using the facet interface. Does anyone know if there is a syntax I can use, specifically in the search URL, to OR my filters/facets? Meaning, to make it such that the result is all entries that contains EITHER of the two filters?
Thanks in advance for any help or pointers in the right direction!
New edit:
I do know how to OR terms within the same vocabulary through the URL, I'm just wondering how to do it for terms in different vocabularies. ;-)
You can write a filter query that looks like:
fq=field1:value1 OR field2:value2
Alternatively you can use localparams to specify the query operator:
fq={!q.op=OR}field1:value1 field2:value2
As far as I know, there's not any easier way to do this. There is, in fact, an rather old bug asking for a way to OR the fq parameters...
I finally found a way to do this in Drupal
Enable the fq parameter setting.
Go to admin/config/search/apachesolr/[your_search_page]/core_search/edit or just navigate to the settings of the search page you're trying to modify
Check the 'Allow user input using the URL' setting
URL Syntax
Add the following at the end of the URL: ?fq=tid:(16 OR 38), where 16 and 38 are the term ids

Drupal 6 --> Drupal 7 Migration field und's

In drupal 6 to get a node field's value you would do:
$node->field_ajax_override[0]['value']
Now it is:
$node->field_ajax_override['und'][0]['value']
Is this just going to be a pain to migrate or is there a better way?
You can use echo render($content['field_ajax_override']); instead in Drupal 7 (amongst other ways)
The article Rendering Drupal 7 Fields the right way discusses why accessing variables through ['und'] is bad. See the below excerpt.
Firstly, the ['und'] element is part of the field localisation in
Drupal 7 (see this article from Gábor Hojtsy for more on that);
directly accessing that value will cause issues in any kind of
multi-lingual environment. Boo.
By accessing the field value directly you miss out on any theming that
might come courtesy of the normal field markup.
The [0][safe_value] explicitly accesses the first value of the field -
if you wanted every value from a multi-value field you'd need to do
some sort of loop.
Some fields (such as node references) won't have a safe_value element,
only a value - which can easily be printed without thought for
sanitisation. This is dangerous, not because node reference fields
contain dangerous data (they're just a nid), but because it's not a
helpful habit to get into, especially for new developers. Other fields
types 'value' may well be highly dangerous.
The article then goes on to advocate the use of field_view_field('node', $node, 'field_name'); but in most cases render($content['field']); will be sufficient, particularly if you already have access to the node you're rendering, for example in the node.tpl.php file or one of it's variations.

How do I prevent a content type from appearing in collections?

How do I prevent a specific content type from appearing in collections (smart folders) site wide in Plone 3? I've looked for relevant options in portal_types and the types and search control panel (turning off the content type for searching doesn't seem to have an effect on collections).
Specific situation: I recently installed plone.app.discussion on a Plone 3.3.5 installation and now comments appear in all the collections. We want to remove them from the collections.
Thanks.
Portal Types criterion is based on plone.app.vocabularies.ReallyUserFriendlyTypes, a vocabulary factory defined in http://svn.plone.org/svn/plone/plone.app.vocabularies/trunk/plone/app/vocabularies/types.py.
If you patch BAD_TYPES by adding discussion comments you'll exclude them from Collections, but you'll also hide them from anywhere this vocabulary factory is used. As far as I know they are also used in contentrules and search control panel.
You can patch BAD_TYPES by adding these lines into __init__.py file of a custom package:
def initialize(context):
"""Initializer called when used as a Zope 2 product."""
from plone.app.vocabularies import types
types.BAD_TYPES = types.BAD_TYPES + ('Discussion Item',)
If you don't have too many collections, the simplest solution might be to add criteria to exclude comments. You can easily get a list of all your collections:
http://your-site/search?portal_type%3Alist=Topic
If you have a lot of collections, you might need to write some custom code to do this. It doesn't have to be a product or anything serious, just some code you can run to add an 'exclude comments' criteria to existing collections. I would start by looking around line 507 of http://svn.plone.org/svn/collective/Products.ATContentTypes/branches/1.3/Products/ATContentTypes/tests/test_criteria.py
You can add criteria to your collections stating which content types you want to display. You can not (without patching/hacking) choose which to exclude.
I.e collections have can have type whitelilsts not blacklists.

Where in code do I add taxonomy_save_vocabulary for drupal module

I know that in order to programmatically add my own taxonomy to a node I need to use the taxonomy_save_vocabulary function and pass it an array defining my vocabulary. But I need to know where does this code usually go in the module. I'm assuming the install file?
Thanks!
D
The install file is for initial set up of your module - setting up database tables and clearing up for uninstall, basically. So yes, if you're adding a taxonomy just once, and especially if it's indispensible to your module, this would be a good place to do it.
More ideas about this here:
http://sachachua.com/wp/2009/04/drupal-staging-and-deployment-tips-its-all-code/

How to find which menu a node belongs to in drupal

I currently have nodes setup on my site, and each node belongs to a particular menu (not primary or secondary prebuilt menues).
How can i find out which menu a node belongs to?
Maybe this is what you mean:
$trail = menu_get_active_trail();
$lastInTrail = end($trail);
$menu_name = $lastInTrail['menu_name'];
menu_get_active_trail() returns a breadcrumbs like array, the last breadcrumb represents the current node.
Cheers,
Laurens Meurs, Rotterdam
I'm a noob, so don't bash me if what I'm going to write is worthless babbling.
I think you can't do that directly, unless there's some smart module out there that would do all the nasty SQL queries necessary to check this.
Node info is stored in the SQL table "node", and is identified merely by NID (node ID, which is the node number that appears after /?q=node/ in the address). Their aliases, if any, are stored in "url_alias" table, where you can find columns "src" and "dst", identifying the original and the aliased path (for instance, src='node/123', dst='my/url/alias'). The menu links can be found in the table "menu_links", where you can find the columns "menu_name" (the machine-radable name of a menu) and "link_path" (either the node/... or the alias).
So, what you'd need to do is the following:
get the current node's NID
query "url_alias" if there's an alias for node/NID and retrieve it, otherwise leave node/NID
query the "menu_links" table for the path you've determined and retrieve "none" or the menu's machine-readable name
You could then also query the table "menu_custom" to check what's the human-readable name of the menu you've determined.
Anyway, that's a complicated query (several queries?) and I'm a MySQL ignorant, so I can't help you with the actual code you'll need to use to check all that :P.
This isn't a direct solution and I see from your reply to a previous answer that you didn't wanted the simplest solution possible, but I thought I'd mention this option. The Menu Node API module maintains a table which Drupal lacks, a node-menu relationship table.
The module does nothing on its own, but there seems to be contributed modules which build on this, so depending on how complex your problem is it might be a starting point.
http://drupal.org/node/584984
Updated: Sorry guys, didn't even realize I had posted this link. I think I intended it as a draft and simply posted it when closing tabs. That said, mingos (above) is right on.
My link is to a function menu_get_active_menu_name() that appears to provide you with an array containing the active menu for the current page. As I presume that is what you are using it for, it would be a nice way to abstract yourself away from the database calls that might cause problems down the line.
I myself have never tried it, which is probably why I didn't elaborate and post. well... at least didn't post on purpose.

Resources