How do use a custom datatype as a field in drupal 7?
I have several custom datatypes in drupal 7
- restaurant
- menu
- recipe
- chef
i want to create associations between this data ch开发者_JS百科ef -> restaurant -> menu -> recipe so that recipe can get the chefs name and the restaurants address and menu can get a list of recipes, etc
In SQL land, i'd call this a foreign key, but i'm having a lot of problems finding how to do this in drupal 7.
I suspect that there must be some module or functionality i'm not familiar with. But drupal uses it's own lingo and I think i'm falling down on my google-fu
anyone know what i'm looking for ?
These are probably not datatypes but most likely content types. What you are looking for are
- A way to relate nodes (ie. content) of one content type to nodes of another (chef to restaurant, menu to restaurant, recipe to menu, etc.).
- A way to display information from related nodes (direct or indirect relations) when displaying a particular node.
The relation between nodes can be achieved using the References module (a Drupal 7 port of the nodereference
module included in CCK for Drupal 6). This allow you to add references to nodes when editing another. The relation can be configured to allow only nodes of the specified type to be referenced. These kind of reference aren't bi-directional and can only (easily) navigated from the referrer node, not from the referenced node. In Drupal 6, the Node referrer provided a complementary field to navigate the relation both way. Unfortunately, it has not (yet) been ported to Drupal 7.
References may be deprecated in a near future in favor of the Entity reference module. Entity reference clains to provide bi-directional queries via views and proper integration with the Entity API module. The later should ensure that relation defined with the module are fully (problematically) navigable and usable with modules using the Entity metadata (Seach API and others).
Another solution which is also using the Entity system, is the Relation module. It provide complete bi-directional relations. These relations are themselves fieldable entitiesm which means that you add properties to the relations (for instance the dates at which a chef started and ended working at a restaurant).
Once you get the relation, there is various ways to display the related information on a node page. I don't known for Relation, but if References behave like the Drupal 6 version, it will only allow basics display of information from the directly related nodes. One solution is to implement hook_node_view()
in a custom module to navigate the relation, retrieve the different nodes, format the gathered information using a custom theme hook and add it to the $node->content
for rendering.
Since Entity reference integrates with view, you should be able to build a view to display the node related to the currently displayed node (using the current node as contextual argument). You can then embed the view programmatically in your node view (again, via hook_node_view()
) or use a block display.
In Drupal 6, you would use a sub-module of the CCK project called Node Reference. A node reference is a field in one content type that points to another node. In Drupal 7, since the majority of CCK's functionality has been moved to core, the Node Reference submodule is now part of the References project.
精彩评论