Hi% username%,
In response to the
“Nontrivial task for the Node Reference fields of Drupal” , I decided to file one of the custom modules for cooking.
For the operation of the module, the following structure of site materials is assumed:
Type of material "Recipe" - the main content with a description of the recipe
Types of materials with ingredients:
- Ingredient 1 - Site materials with the ingredients of the first group (eg. Material type "Spices")
- Ingredient 2 - Site materials with the ingredients of the second group (eg the type of material "Fruit")
- ...
- Ingredient N - Site materials with ingredients of the N group
All ingredients in the drop-down list will be divided into groups. So it will be easy to find them.
After switching on the module, an additional type, “Ingredient”, will appear in the list of available field types.
The module allows you to add reference references to the nodes of ingredients, set the quantity of the product and units of measurement. Supports multiple lines adding, drag & drop sorting.
')
You only need to specify which types of materials are ingredients.
A useful feature of the module is a function of the theization of the displayed data.
function theme_recipe_site_formatter_default ($ element) {
$ fields = $ element ['# item'];
$ res = '';
if (! empty ($ fields)) {
$ item = explode ('[nid:', $ fields ['ingredient_name']);
$ title = trim ($ item [0]);
$ nid = drupal_substr ($ item [1], 0, -1);
$ res = l ($ title, 'node /'. $ nid). ' - '. $ fields ['ingredient_qty']. ($ fields ['ingredient_unit'] == '<space>'? '': ''. $ fields ['ingredient_unit']);
}
return $ res;
}
In the template.php theme file, we create our own function and form the $ res variable as a link to the previously prepared views, to which the nid of the ingredient is passed as an argument. This will provide a list of all recipes that use this ingredient:
function FILES_recipe_site_formatter_default ($ element) {
$ fields = $ element ['# item'];
$ res = '';
if (! empty ($ fields)) {
$ item = explode ('[nid:', $ fields ['ingredient_name']);
$ title = trim ($ item [0]);
$ nid = drupal_substr ($ item [1], 0, -1);
$ res = l ($ title, 'PATH_K_VYUS /'. $ nid). ' - '. $ fields ['ingredient_qty']. ($ fields ['ingredient_unit'] == '<space>'? '': ''. $ fields ['ingredient_unit']);
}
return $ res;
}
Do not forget to reset the cache.
Actually, the
module itself
Oddly enough - it works;)
An article for inviting my friend, not yet a user of Habrahabr.
If there are good people, send him to serjik.bozhko@gmail.com, please, an invitation =).