Let's suppose you are using the Entity Reference Views Widget to display items to be included in an Entity Reference field. This is actually quite a cool module while being a little awkward to use - essentially it gives you a views listing of candidate entities to add, with an AJAX-driven checkbox: click the box and the entity gets moved to the list on the left to display what's been chosen.
Which is all fine.
The project I'm currently working on uses Organic Groups to group certain users within an organisation allowing them to work on very specific types of node content. I had to create a completely new type of entity (though that's not important) for them so they could add one or more of these entities to one or more of their special content.
The new entity was made subject to the OG, and that too was fine.
So then I came to build the Entity Reference Views Widget to only display the new entities that belonged to the current user's OG.
Meltdown. Either I listed everything, or nothing. Filtering OGs is not the easiest thing in the world: You have to create an OG relationship for the entity in question (easy) and then add an argument which if it has no value (the desired state) uses the OG Context module to figure out what OGs are available.
Weird fact number #87654: The OG context module allows you to base OG on the current node and on a user currently being viewed or edited, but not on the current user. So I had to build a quick context for that:
/**
* Implements hook_og_context_negotiation_info().
*/
function mymodule_og_context_negotiation_info() {
return array(
'user' => array(
'name' => t('User'),
'description' => t("Determine context by finding the current user's OG (if any)."),
'callback' => 'mymodule_context_handler_user',
),
);
}
/**
* Implements hook_og_context_negotiation_info_alter().
*/
function mymodule_og_context_negotiation_info_alter(&$contexts) {
$context['node']['menu path'][] = 'node/%/edit';
}
function mymodule_context_handler_user() {
global $user;
$account = clone $user;
$contexts = _group_context_handler_entity('user', $account);
return $contexts;
}
In fact this does two things: it expands the context checking for nodes to include nodes being edited and adds a context that looks at the current user.
Okay. Next factor: Entity Reference Views Widget has this neat facility for feeding the entity IDs of entities already selected back into the view and excluding them. This is great and it also uses an argument, which needs to be the first argument.
However, and this is the nastiness, if the ERVW argument does not exist (i.e. no entities have yet to be selected for the field) the second argument fails to fire and you see all the entities without any OG filtering.
The solution is thankfully quite simple: Edit the ERVW argument so that it has a default value of "all", this means it always exists and the second argument does fire and figure out the correct OG context puts it into the query and filtering actually works.
Obviously I went through various stages of thinking each handler was broken or that there was something weird about my newly created entity. But none of those things were true. The problem was simply one of configuration.
Hope that helps.
Showing posts with label entities. Show all posts
Showing posts with label entities. Show all posts
Friday, 5 July 2013
Wednesday, 4 July 2012
Changing view_mode mid-stream
In my current contract I have to display a hierarchy of complex taxonomy terms - each one as a page with relevant data hanging off it. There are three levels to the taxonomy: Level 1 displays one set of data and links, Level 2 is never displayed on its own (which is something I'll have to take care of) and Level 3 has a different page structure again.
Different page layouts means Display Suite (http://drupal.org/project/ds) and that's great. It works fine when you want to configure a different layout for a node - as long as it's the same for every node type. Or entity bundle.
DS works fine for taxonomy terms in just the same way as for nodes. It is one of my favourite modules.
But I needed to be able to change the view_mode on the fly: to check what level the taxonomy term is and change the view_mode based on that. Which is when I ran into trouble - and it's not DS's fault. Though I never had to do it apparently this was not a problem in D6 but in D7 there is a bug which makes it tricky to change view_mode. You can read all about it here: http://drupal.org/node/1154382
This issue gives some hints as to the solution (hook_entity_prepare_view() is not it), but there is a link to this blog here. The solution described is for changing build mode for nodes based on the current theme (so you can change things if you're using a mobile theme).
My solution is the same except slightly more generic, instead of intercepting hook_node_...() hooks, I intercept hook_entity_...() hooks.
I'm just going to throw my code at you because I know you can work out what to do in your own situation.
/**
* Implements hook_entity_prepare_view().
*
* We have to play silly games to change the view_mode, first we intercept
* hook_entity_prepare_view() and establish what view_mode we want, and
* save it. But there's a bug which means this has no effect on the output
* so...
*/
function page_g2g_entity_prepare_view($entities, $entity_type, $langcode) {
if ($entity_type!='taxonomy_term') {
return;
}
foreach ($entities as $id => $entity) {
if ($entity->vocabulary_machine_name!='gtg_tags' || $entity->view_mode!='full') {
// wrong vocabulary or not a "full page"
continue;
}
// Change the display dependent on the number of parents
switch (count(taxonomy_get_parents_all($entity->tid))) {
case 1:
$entity->view_mode = 'g2g_level_1';
break;
case 2:
// Hm. Need to do something else here, maybe
// do a redirect to the parent term.
break;
case 3:
$entity->view_mode = 'g2g_level_3';
break;
}
}
}
/**
* Implements hook_entity_view_alter().
*
* ...we intercept before the full build is enacted. You can test and see that
* even though we changed the view_mode in the term itself, it hasn't transferred
* to the build theme. So having verified we want to do it with this entity
* we transfer it. And now it gets changed.
*/
function page_g2g_entity_view_alter(&$build) {
if (isset($build['#entity_type']) && $build['#entity_type']=='taxonomy_term') {
$build['#view_mode'] = $build['#term']->view_mode;
}
}
Arguably you don't need the first hook, you could do it all in the second call. But it's a matter of elegance and splitting actions into their appropriate locations.
Different page layouts means Display Suite (http://drupal.org/project/ds) and that's great. It works fine when you want to configure a different layout for a node - as long as it's the same for every node type. Or entity bundle.
DS works fine for taxonomy terms in just the same way as for nodes. It is one of my favourite modules.
But I needed to be able to change the view_mode on the fly: to check what level the taxonomy term is and change the view_mode based on that. Which is when I ran into trouble - and it's not DS's fault. Though I never had to do it apparently this was not a problem in D6 but in D7 there is a bug which makes it tricky to change view_mode. You can read all about it here: http://drupal.org/node/1154382
This issue gives some hints as to the solution (hook_entity_prepare_view() is not it), but there is a link to this blog here. The solution described is for changing build mode for nodes based on the current theme (so you can change things if you're using a mobile theme).
My solution is the same except slightly more generic, instead of intercepting hook_node_...() hooks, I intercept hook_entity_...() hooks.
I'm just going to throw my code at you because I know you can work out what to do in your own situation.
/**
* Implements hook_entity_prepare_view().
*
* We have to play silly games to change the view_mode, first we intercept
* hook_entity_prepare_view() and establish what view_mode we want, and
* save it. But there's a bug which means this has no effect on the output
* so...
*/
function page_g2g_entity_prepare_view($entities, $entity_type, $langcode) {
if ($entity_type!='taxonomy_term') {
return;
}
foreach ($entities as $id => $entity) {
if ($entity->vocabulary_machine_name!='gtg_tags' || $entity->view_mode!='full') {
// wrong vocabulary or not a "full page"
continue;
}
// Change the display dependent on the number of parents
switch (count(taxonomy_get_parents_all($entity->tid))) {
case 1:
$entity->view_mode = 'g2g_level_1';
break;
case 2:
// Hm. Need to do something else here, maybe
// do a redirect to the parent term.
break;
case 3:
$entity->view_mode = 'g2g_level_3';
break;
}
}
}
/**
* Implements hook_entity_view_alter().
*
* ...we intercept before the full build is enacted. You can test and see that
* even though we changed the view_mode in the term itself, it hasn't transferred
* to the build theme. So having verified we want to do it with this entity
* we transfer it. And now it gets changed.
*/
function page_g2g_entity_view_alter(&$build) {
if (isset($build['#entity_type']) && $build['#entity_type']=='taxonomy_term') {
$build['#view_mode'] = $build['#term']->view_mode;
}
}
Arguably you don't need the first hook, you could do it all in the second call. But it's a matter of elegance and splitting actions into their appropriate locations.
Thursday, 12 April 2012
Entities vs Nodes
For the last six months I've been working on a Drupal 6 site professionally, but developing a couple of personal Drupal 7 sites. However I haven't been going into D7 in any great detail - except for developing entities.
Now I've moved contracts and I'm developing a new Drupal 7 site to replace an older non-Drupal site with lots of additional facilities. And the Drupal implementation is entirely up to me.
Being an OOP person at heart I love the concept of entities but when working with a commercial website you have to make some serious decisions. My personal preferences have to give way to the reality of building a website that delivers the spec and can be maintained and extended by other developers in the future.
So how do you decide what should be a node and what should be an entity?
There's a line in this blog post which says:
1. Is it content? Is the item definitely stuff that gets turned into HTML and displayed for the user? If you were building a review site, a review would definitely be content, so that's a node.
2. Does it need to have revisions? The node module provides revisions and the associated modules make it easy and powerful. Building revisions into custom entities is hard. So if it has to have revisions then it has to be a node.
3. Is there additional "property" (as opposed to "field") information? I had a situation where I wanted to define a "proxy", and a proxy needs a web address, a port number, maybe a username and password. These are fundamental properties of a proxy. You could add these as fields for a node, of course, but it makes more sense for them to be properties of a proxy entity. So this should be considered as an entity. (Another way of looking at this is: is there a need for a new table containing information specific to this data structure? If so, think entity.)
4. Is the structure normally invisible to the user? That should be an entity.
5. Would using an entity instead of a node obscure the function? Perhaps this is tricky to answer, after all dividing functionality out to a new object should never make things more complex. But it's worth asking the question.
Any other ways of to help make the decision?
Remember: there is virtually no overhead in creating a new entity. And there are huge advantages in additional functionality that the core, Entity API, Views, Features and other entity-related modules can give you.
Now I've moved contracts and I'm developing a new Drupal 7 site to replace an older non-Drupal site with lots of additional facilities. And the Drupal implementation is entirely up to me.
Being an OOP person at heart I love the concept of entities but when working with a commercial website you have to make some serious decisions. My personal preferences have to give way to the reality of building a website that delivers the spec and can be maintained and extended by other developers in the future.
So how do you decide what should be a node and what should be an entity?
There's a line in this blog post which says:
You can now actually create data structures specific to your application domain with their own database table (or any other storage mechanism really) plus a standardised way to add fields to them. No need to turn nodes into something they are not.Which is technically accurate but does not always provide clear guidance, so here's my step-by-step analysis method to decide whether a specific data structure should be a node or not:
1. Is it content? Is the item definitely stuff that gets turned into HTML and displayed for the user? If you were building a review site, a review would definitely be content, so that's a node.
2. Does it need to have revisions? The node module provides revisions and the associated modules make it easy and powerful. Building revisions into custom entities is hard. So if it has to have revisions then it has to be a node.
3. Is there additional "property" (as opposed to "field") information? I had a situation where I wanted to define a "proxy", and a proxy needs a web address, a port number, maybe a username and password. These are fundamental properties of a proxy. You could add these as fields for a node, of course, but it makes more sense for them to be properties of a proxy entity. So this should be considered as an entity. (Another way of looking at this is: is there a need for a new table containing information specific to this data structure? If so, think entity.)
4. Is the structure normally invisible to the user? That should be an entity.
5. Would using an entity instead of a node obscure the function? Perhaps this is tricky to answer, after all dividing functionality out to a new object should never make things more complex. But it's worth asking the question.
Any other ways of to help make the decision?
Remember: there is virtually no overhead in creating a new entity. And there are huge advantages in additional functionality that the core, Entity API, Views, Features and other entity-related modules can give you.
Sunday, 11 September 2011
field_extract module in beta
EDIT: Now out of beta. Fully implemented.
EDIT: You can now get the module from http://drupal.org/project/field_extract and it works properly with Drush.
My incredibly useful field_extract module is now available on drupal.org. I am slightly embarrassed because something went wrong in the project creation process so the actual URL is:
http://drupal.org/project/1158878
And when you download it the module comes inside another folder "1158878". In fact this will work just as it comes, you'll just get the folder 1158878 in your modules folder with field_extract inside that.
If you're competent you can extract the inner "field_extract" module and get rid of the outer 1158878 folder but, as I say, it should work out of the box anyway. (I may get around to re-uploading it properly at some point but I can't wait any longer to get it out to you.)
So what does it do? For a start, this is only a developer's module. It does nothing by itself and should only be downloaded if requested by another module. But what it does do is provide a couple of functions that make it much easier to extract field data from entities.
The project page provides clear instructions on how to use it.
Enjoy.
EDIT: You can now get the module from http://drupal.org/project/field_extract and it works properly with Drush.
My incredibly useful field_extract module is now available on drupal.org. I am slightly embarrassed because something went wrong in the project creation process so the actual URL is:
http://drupal.org/project/1158878
And when you download it the module comes inside another folder "1158878". In fact this will work just as it comes, you'll just get the folder 1158878 in your modules folder with field_extract inside that.
If you're competent you can extract the inner "field_extract" module and get rid of the outer 1158878 folder but, as I say, it should work out of the box anyway. (I may get around to re-uploading it properly at some point but I can't wait any longer to get it out to you.)
So what does it do? For a start, this is only a developer's module. It does nothing by itself and should only be downloaded if requested by another module. But what it does do is provide a couple of functions that make it much easier to extract field data from entities.
The project page provides clear instructions on how to use it.
Enjoy.
Labels:
drush,
entities,
field api,
field extract,
field values,
field_get_items,
file,
node,
term,
user,
vocabulary
Subscribe to:
Posts (Atom)