I am using Drupal 7 and I need to show a different content for a custom entity depending on if the recently created content is being shown after submitting the form or if I visit the URL afterwards.

Is there a way to have different templates or is there a variable to check that tells you if it's newly created content?

I need this because I'm going to let the user share the page on social networks from that page, and I need to show a different content when people visit it afterwards.

  • Let me see if I understand the question. You want the author of the content to be able to share what he/she just created on a social network, but you want other visitors not to see the share link? Or is there any other major difference in look such as a different theme for the author and the later visitors? Also, are you looking to do this programmatically or through modules?
    – argiepiano
    Apr 22 '17 at 14:34
  • Yes more or less, but not only the links to share the page, I need to change the aspect of the page and hide some other things. The problem is that the author is going to be anonymous, users are not registering to create the content. So I need to show something different when the user sees the page for the first time.I want to do it from my custom module or theme.
    – jmzea
    Apr 22 '17 at 14:38
  • It would be helpful if you could include a rough, simple mock of the look for the author and for the visitor in your original post. Also: should the author be able to see the "creator" look when he/she visits the page later? This may be tricky, as this is an anonymous user...
    – argiepiano
    Apr 22 '17 at 14:44
  • Just imagine that when the anonymous user create the content some fields are shown including the links, and next time the page is visited by the same person or another different fields are displayed. That's why I wonder if there is a hook to check if we are seeing the new entity created after the submit button has been pressed and the form validates, so I can set a Boolean variable to check in the template and alter the content.
    – jmzea
    Apr 22 '17 at 14:49
  • Let us continue this discussion in chat.
    – argiepiano
    Apr 22 '17 at 15:21

Here's one way to solve this. In a nutshell:

  1. Create a custom view for your entity using Display Suite or Entity View Modes. Display Suite is preferred, as it allows you to also create custom fields and custom layouts for your entity display. This is the custom view your authors will see when they submit the entity create form.

  2. Alter the entity creation form and add a new submit handler (be sure not to delete the default submit handler, as that will take care of creating and saving the entity before your custom handler is called).

  3. In the new submit handler, create a form_state['redirect'] to a custom menu item

  4. Create a custom menu item (with hook_menu) that will load your entity and pass it to a page callback

  5. The page callback will build and return the entity custom view you defined in step 1.

Here's the code for all this (except step 1). PLEASE NOTE: since I don't know what custom entity you are using, this example uses Basic page node (content) type. You will need to adapt to your custom entity. All this code goes in a custom module called my_custom_view.module.

Also note: the custom view I created in Display Suite is called custom (see node_view() in the page callback).


function my_custom_view_form_page_node_form_alter(&$form, $form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = 'my_custom_view_form_submit';

function my_custom_view_form_submit($form, &$form_state) {
  $form_state['redirect'] = 'custom-node-view/'.$form_state['nid'];

function my_custom_view_menu() {
  $item = array();
  $item['custom-node-view/%node'] = array(
    'page callback' => 'my_custom_view_page',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
  return $item;

function my_custom_view_page($node) {
  return node_view($node, 'custom');
  • Hi @argiepiano. First of all, thank you for your detailed reply and explanation. I've tried your code, but I got the message 'The requested page could not be found', I wonder if it's because it's a custom entity and not a node, and the because of the wildcard %node Drupal is trying to load a node automatically. I tried to look for a equivalent for an entity but I could find any.
    – jmzea
    Apr 24 '17 at 8:48
  • The issue is that you need to replace %node with the correct loader for your entity. I'm curious: what entity are you using? Is it a custom one created by a custom module with hook_entity_info()? Not only will you have to change %node but also a LOT more of the code (for example, don't use node_view() in the page callback). Generally, if the entity was created with Entity API, the loader is called %entity_name (replace entity_name by the name of your entity). For example if your entity is called student, use %student and student_load, etc etc.
    – argiepiano
    Apr 24 '17 at 12:31
  • Ok, I'll test it. It's a custom entity created with the module ECK.
    – jmzea
    Apr 24 '17 at 12:36
  • I can't get it. Same message. I don't know what I'm doing wrong. It's an entity defined with ECK, I tried using the name of the bundle and the name of the entity.
    – jmzea
    Apr 24 '17 at 13:06
  • Clear caches to rebuild menus? I would be willing to invest a bit more time than I've done so far, but there is so much I can do without you entity. My solution works, but you need the know-how to adapt it to your siruation. Is there a way to export ECK entities? Can you send it ?
    – argiepiano
    Apr 24 '17 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.