edited tags
Link
apaderno
  • 95.5k
  • 15
  • 158
  • 283
replaced http://drupal.stackexchange.com/ with https://drupal.stackexchange.com/
Source Link

Some pages (paths) represent one entity. For example:

  • The pages at these paths represent one node entity, entity_id (nid) is XX:
  • /node/xx
  • /node/xx/delete
  • /node/xx/edit
  • The page at this path represents one search server entity (defined by search api module), entity_id (machine_name) is 'apache_solr':
  • /admin/config/search/search_api/server/apache_solr

How do you load in the basic data about this entity (id and type) when the hook or function you are in doesn't hand it to you?

The standard way to get the current entity is apparently to useis apparently to use menu_get_object($entity_type,$position_of_id_in_url). This, however, assumes that you are coding for just one entity type: you need to state the entity type programmatically, and, even worse, you the human being need to research and hard code in the url position of entity ids that this entity uses.

The closest I can find so far is getting the underlying system uri for the page (e.g. substr(request_uri(),strlen($base_path))), then writing a monsterous switch list based on something like this suggestionsomething like this suggestion, guessing and then hard coding cases for every possible entity you think a site might ever have. But there must be something better than that, right?

While a general solution from core would be best, I'll also accept an answer that depends on common modules like ctools (page manager, contexts, etc), Entity API, Devel, etc.

My immediate need is for edit forms, within hook_field_widget_form(), for a widget that can (with appropriate config and modules) be attached to any fieldable entity type. So, while a general solution would be best, I would also accept a narrow answer that only works within this function or in forms (The id and data that is specific to entity type appears to be buried in $form, but keyed in an entity type specific way, so getting it out of $form seems like a chicken and egg problem).

------------ EDIT ------------------

Seems some people have difficulty understanding the simple general problem. Here's an alternate use case (another seperate but related problem I'm facing). Suppose you have a view that lists some entities. This view sits as a block/pane on pages that can represent one entity, such as a node page or a user profile (or both). You've got a hook set up that reads the views data and looks through the results (foreach $view->result as...). Let's say it's hook_views_pre_render, which means you just have the $view object. You want to do something (say, remove the row or change some field data) if the view results row has the same entity id and entity type as the page does. You've got the logic down to extract this data from the view results, so all you need is to look up the entity_type and entity_id of the page being generated, to make a comparison. That data is in Drupal somewhere. If it wasn't, Drupal couldn't have figured out that it needed to load this view. So how do you get it when it isn't passed to the hook or function you are in?

Some pages (paths) represent one entity. For example:

  • The pages at these paths represent one node entity, entity_id (nid) is XX:
  • /node/xx
  • /node/xx/delete
  • /node/xx/edit
  • The page at this path represents one search server entity (defined by search api module), entity_id (machine_name) is 'apache_solr':
  • /admin/config/search/search_api/server/apache_solr

How do you load in the basic data about this entity (id and type) when the hook or function you are in doesn't hand it to you?

The standard way to get the current entity is apparently to use menu_get_object($entity_type,$position_of_id_in_url). This, however, assumes that you are coding for just one entity type: you need to state the entity type programmatically, and, even worse, you the human being need to research and hard code in the url position of entity ids that this entity uses.

The closest I can find so far is getting the underlying system uri for the page (e.g. substr(request_uri(),strlen($base_path))), then writing a monsterous switch list based on something like this suggestion, guessing and then hard coding cases for every possible entity you think a site might ever have. But there must be something better than that, right?

While a general solution from core would be best, I'll also accept an answer that depends on common modules like ctools (page manager, contexts, etc), Entity API, Devel, etc.

My immediate need is for edit forms, within hook_field_widget_form(), for a widget that can (with appropriate config and modules) be attached to any fieldable entity type. So, while a general solution would be best, I would also accept a narrow answer that only works within this function or in forms (The id and data that is specific to entity type appears to be buried in $form, but keyed in an entity type specific way, so getting it out of $form seems like a chicken and egg problem).

------------ EDIT ------------------

Seems some people have difficulty understanding the simple general problem. Here's an alternate use case (another seperate but related problem I'm facing). Suppose you have a view that lists some entities. This view sits as a block/pane on pages that can represent one entity, such as a node page or a user profile (or both). You've got a hook set up that reads the views data and looks through the results (foreach $view->result as...). Let's say it's hook_views_pre_render, which means you just have the $view object. You want to do something (say, remove the row or change some field data) if the view results row has the same entity id and entity type as the page does. You've got the logic down to extract this data from the view results, so all you need is to look up the entity_type and entity_id of the page being generated, to make a comparison. That data is in Drupal somewhere. If it wasn't, Drupal couldn't have figured out that it needed to load this view. So how do you get it when it isn't passed to the hook or function you are in?

Some pages (paths) represent one entity. For example:

  • The pages at these paths represent one node entity, entity_id (nid) is XX:
  • /node/xx
  • /node/xx/delete
  • /node/xx/edit
  • The page at this path represents one search server entity (defined by search api module), entity_id (machine_name) is 'apache_solr':
  • /admin/config/search/search_api/server/apache_solr

How do you load in the basic data about this entity (id and type) when the hook or function you are in doesn't hand it to you?

The standard way to get the current entity is apparently to use menu_get_object($entity_type,$position_of_id_in_url). This, however, assumes that you are coding for just one entity type: you need to state the entity type programmatically, and, even worse, you the human being need to research and hard code in the url position of entity ids that this entity uses.

The closest I can find so far is getting the underlying system uri for the page (e.g. substr(request_uri(),strlen($base_path))), then writing a monsterous switch list based on something like this suggestion, guessing and then hard coding cases for every possible entity you think a site might ever have. But there must be something better than that, right?

While a general solution from core would be best, I'll also accept an answer that depends on common modules like ctools (page manager, contexts, etc), Entity API, Devel, etc.

My immediate need is for edit forms, within hook_field_widget_form(), for a widget that can (with appropriate config and modules) be attached to any fieldable entity type. So, while a general solution would be best, I would also accept a narrow answer that only works within this function or in forms (The id and data that is specific to entity type appears to be buried in $form, but keyed in an entity type specific way, so getting it out of $form seems like a chicken and egg problem).

------------ EDIT ------------------

Seems some people have difficulty understanding the simple general problem. Here's an alternate use case (another seperate but related problem I'm facing). Suppose you have a view that lists some entities. This view sits as a block/pane on pages that can represent one entity, such as a node page or a user profile (or both). You've got a hook set up that reads the views data and looks through the results (foreach $view->result as...). Let's say it's hook_views_pre_render, which means you just have the $view object. You want to do something (say, remove the row or change some field data) if the view results row has the same entity id and entity type as the page does. You've got the logic down to extract this data from the view results, so all you need is to look up the entity_type and entity_id of the page being generated, to make a comparison. That data is in Drupal somewhere. If it wasn't, Drupal couldn't have figured out that it needed to load this view. So how do you get it when it isn't passed to the hook or function you are in?

Tweeted twitter.com/StackDrupal/status/661105360233742336
Tried to improve clarity, added an alternate example to hopefully help people understand the general case
Source Link

Some pages (paths) represent one entity. For example:

  • The pages at these paths represent one node entity, entity_id (nid) is XX:
  • /node/xx
  • /node/xx/delete
  • /node/xx/edit
  • The page at this path represents one search server entity (defined by search api module), entity_id (machine_name) is 'apache_solr':
  • /admin/config/search/search_api/server/apache_solr

How do you load in the basic data about this entity (id and type) when the hook or function you are in doesn't hand it to you?

The standard way to get the current entity is apparently to use menu_get_object($entity_type,$position_of_id_in_url). This, however, assumes that you are coding for just one entity type: you need to state the entity type programmatically, and, even worse, you the human being need to look upresearch and specificallyhard code in the url position of entity ids that this entity uses...

What can you do if your code can be executed from a range of different entity types, and when entity_type is one of the types of information you need to retreive?

The closest I can find so far is getting the underlying system uri for the page somehow(e.g. substr(request_uri(),strlen($base_path))), then writing a monsterous switch list based on something like this suggestion, guessing and then hard coding cases for every possible entity you think a site might ever have, but. But there must be something better than that, right?

While a general solution from core would be best, I'll also accept an answer that depends on common modules like ctools (page manager, contexts, etc), Entity API, Devel, etc.

My immediate need is for edit forms, within hook_field_widget_form(), for a widget that can (with appropriate config and modules) be attached to any fieldable entity type. So, while a general solution would be best, I would also accept a narrow answer that only works within this function or in forms (The id and data that is specific to entity type appears to be buried in $form, but keyed in an entity type specific way, so getting it out of $form seems like a chicken and egg problem).

------------ EDIT ------------------

Seems some people have difficulty understanding the simple general problem. Here's an alternate use case (another seperate but related problem I'm facing). Suppose you have a view that lists some entities. This view sits as a block/pane on pages that can represent one entity, such as a node page or a user profile (or both). You've got a hook set up that reads the views data and looks through the results (foreach $view->result as...). Let's say it's hook_views_pre_render, which means you just have the $view object. You want to do something (say, remove the row or change some field data) if the view results row has the same entity id and entity type as the page does. You've got the logic down to extract this data from the view results, so all you need is to look up the entity_type and entity_id of the page being generated, to make a comparison. That data is in Drupal somewhere. If it wasn't, Drupal couldn't have figured out that it needed to load this view. So how do you get it when it isn't passed to the hook or function you are in?

The standard way to get the current entity is apparently to use menu_get_object($entity_type,$position_of_id_in_url). This, however, assumes that you are coding for just one entity type: you need to state the entity type programmatically and you need to look up and specifically code in the url position of entity ids this entity uses...

What can you do if your code can be executed from a range of different entity types, and when entity_type is one of the types of information you need to retreive?

The closest I can find so far is getting the underlying system uri for the page somehow, then writing a monsterous switch list based on something like this suggestion, guessing and then hard coding for every possible entity you think a site might ever have, but there must be something better than that, right?

While a general solution from core would be best, I'll also accept an answer that depends on common modules like ctools (page manager, contexts, etc), Entity API, Devel, etc.

My immediate need is for edit forms, within hook_field_widget_form(), for a widget that can (with appropriate config and modules) be attached to any fieldable entity type. So, while a general solution would be best, I would also accept a narrow answer that only works within this function or in forms (The id and data that is specific to entity type appears to be buried in $form, but keyed in an entity type specific way, so getting it out of $form seems like a chicken and egg problem).

Some pages (paths) represent one entity. For example:

  • The pages at these paths represent one node entity, entity_id (nid) is XX:
  • /node/xx
  • /node/xx/delete
  • /node/xx/edit
  • The page at this path represents one search server entity (defined by search api module), entity_id (machine_name) is 'apache_solr':
  • /admin/config/search/search_api/server/apache_solr

How do you load in the basic data about this entity (id and type) when the hook or function you are in doesn't hand it to you?

The standard way to get the current entity is apparently to use menu_get_object($entity_type,$position_of_id_in_url). This, however, assumes that you are coding for just one entity type: you need to state the entity type programmatically, and, even worse, you the human being need to research and hard code in the url position of entity ids that this entity uses.

The closest I can find so far is getting the underlying system uri for the page (e.g. substr(request_uri(),strlen($base_path))), then writing a monsterous switch list based on something like this suggestion, guessing and then hard coding cases for every possible entity you think a site might ever have. But there must be something better than that, right?

While a general solution from core would be best, I'll also accept an answer that depends on common modules like ctools (page manager, contexts, etc), Entity API, Devel, etc.

My immediate need is for edit forms, within hook_field_widget_form(), for a widget that can (with appropriate config and modules) be attached to any fieldable entity type. So, while a general solution would be best, I would also accept a narrow answer that only works within this function or in forms (The id and data that is specific to entity type appears to be buried in $form, but keyed in an entity type specific way, so getting it out of $form seems like a chicken and egg problem).

------------ EDIT ------------------

Seems some people have difficulty understanding the simple general problem. Here's an alternate use case (another seperate but related problem I'm facing). Suppose you have a view that lists some entities. This view sits as a block/pane on pages that can represent one entity, such as a node page or a user profile (or both). You've got a hook set up that reads the views data and looks through the results (foreach $view->result as...). Let's say it's hook_views_pre_render, which means you just have the $view object. You want to do something (say, remove the row or change some field data) if the view results row has the same entity id and entity type as the page does. You've got the logic down to extract this data from the view results, so all you need is to look up the entity_type and entity_id of the page being generated, to make a comparison. That data is in Drupal somewhere. If it wasn't, Drupal couldn't have figured out that it needed to load this view. So how do you get it when it isn't passed to the hook or function you are in?

Remove duplicated tag from title, fix title grammar, remove superfluous stuff
Source Link
user7
user7
Loading
Source Link
Loading