6

I have created a block for showing author details for every individual author; the article's authors can be identified by the 'Author' field. Now I want to match the author block with the particular author's content.

For example, I have an author called 'vividvilla', and I have created a block for 'vividvilla'. How do I show this block for articles where 'vividvilla' is the author?

I know it can be done with PHP code in blocks, but I am not sure about the code.

8
+200

I would use the menu_get_object() function to determine whether or not the block visibility check is being executed on a full node page, then check the field value on the node from there.

EDIT

The other edits were getting a bit long so I've deleted them. I've just set up a site quickly to test this and the following steps have worked for me (this is Drupal 7):

  • Created a content type called Author Profile (machine name author_profile)
  • Added a node reference field from the Article content type to the Author Profile
  • Created an Author Profile node with a title of "vividvilla"
  • Created an Article node linked to the Author Profile node above
  • Enabled the core PHP Filter module
  • Created a block called Author Details with some dummy content in the body, choosing "Pages on which this PHP code returns TRUE (experts only)" for the block visibility settings.
  • Entered the PHP code below

    <?php
    
    $node = menu_get_object();
    
    // Check we're on a 'full' node page.
    if ($node && isset($node->nid)) {
      // Check the node is of the right type
      if ($node->type != 'article') {
        return FALSE;
      }
    
      // Load the author node based on the node reference field value.
      $author_node = node_load($node->field_blog_authors[$node->language][0]['nid']);
    
      // Check the title of that node
      if ($author_node->title == 'vividvilla') {
        return TRUE;
      }
    }
    
    // Default is to not show the block
    return FALSE;
    
    ?>
    

The block only shows on an article page that is linked to an Author Profile with the title of "vividvilla" which I'm pretty sure is what you're trying to.

On the off-chance you're using Drupal 6 just change the $author_node = line to:

$author_node = node_load($node->field_blog_authors[0]['nid']);
  • my field machine name is 'field_blog_authors' and it is a Node reference field. – Vivek R Feb 19 '12 at 15:20
  • @vividvilla I've updated the answer with another example, let me know if I'm still not understanding what you need to do :) – Clive Feb 19 '12 at 15:25
  • You understood my question correctly and the updated answer is exactly what i needed but i have Content type called 'profile' from where the authors field is referenced.What should be changed now ? – Vivek R Feb 19 '12 at 15:41
  • @vividvilla In that case you should just be able to change if ($node->type != 'article') { to if ($node->type != 'profile') {, I'll update the answer :) – Clive Feb 19 '12 at 15:43
  • it is not working,the block is shown at all the articles.The original content type is Article only the author belongs to profile content type.To test the code i changed the author name to some invalid name but then also the block is showing up on articles. – Vivek R Feb 19 '12 at 16:07
7

the most easy way to do it i think is using Views.

In views you can add conditions and get arguments from the url and you can easy set a condition to display the author when you are seeing a node who belongs this author

7

First of all, I would like to address

I know it can be done with PHP code in blocks, but I am not sure about the code.

Please avoid inputting code through the UI in this fashion. Use of the eval() is a large source of problems, including security issues. Just see the big warning in php's own docs. I strongly suggest finding a different approach.

That said, this is easily done with Panels and the Page manager. Please note though, that using both Panels and the Page manager requires quite a bit of understanding for both of them. They are powerful tools, and as such a bit complex.

  1. Install the Chaos tool suite (ctools) and Panels modules.
  2. Enable the Page Manager and Panels.
  3. Enable the node template
  4. Create a variant of type "Panel", add to it a selection rule on the content type that has been authored.
  5. Add a relationship of the type that corresponds to the link between the authored content, and it's author.
  6. Place the authored content in one location, and the authors "content" in another.
  7. Save

Page manager will now automatically derive the author from every displayed piece of content, and render what ever information you have on the authors where you place it.

Video series on using the two modules: Learn Page Manager!.

6

If each post has a Node Reference field pointing at the author, then create a View which displays the author for a particular post, by adding an argument to the view and selecting the author field as the argument.

Create a Block display for that View. Blocks don't have URLs, so can't directly get arguments, but the way around this is to click the argument in the display editing pane and specify an 'Action to take if argument is not present'. One option is 'Provide Default Argument', and you can select 'Node ID from URL'.

Then just arrange for the block to be included on the relevant pages - I tend to do this simply by URL.

This question is actually very similar:

How to show a view of content types on another content type

And my response there has a lot more detail!

5

If you insist in having blocks created with admin/structure/block/add (this means that you are using "semi-static" blocks), I would recommend do it this way:

  1. install the block reference module http://drupal.org/project/blockreference
  2. replace your author field with a block reference field on your content type
  3. edit your node and select the correct block in you block reference field

Now you have 2 options to display the block:

  1. use the field UI to configure and display the block in the node body;
  2. use the field UI to hide the field in the body and then edit your template file and add the following code where you want your block to be displayed:

    print theme('blockreference_item_simple', array('item' => field_get_items('node', $node, 'field_your-field-name-here', $node->language)));
    
4

You can do this without any custom code just using Taxonomy, and the Context module.

Create a taxonomy vocabulary called Authors for instance, and add a term to the list for each author.

For your author's field on the articles, set that as a Term Reference field, and input the appropriate author taxonomy term into there.

With the Context module, you can create a context which has a specific block (select the author's block) to be displayed only when a specific Taxonomy term is present for the currently active node.

4

I will not answer directly to your question since there are plenty of responses with custom code and various modules that you can use in order to achieve the functionality that you want.

My answer will focus on the method that you chose to use. From what I've read, your approach can be summarized as follows:

1) You have x authors in your website

2) You created x number of blocks (one for each author)

3) You are trying to "associate" each block with nodes, based on the author of the node. So, for example, the first block will only appear if the author is "John Doe", the second block ... , the x-th block will only appear if the author is "Jane Doe".

(please correct me if I'm wrong and you are not using the methodology that I described above)

In my opinion, there are some facts about this specific approach that you should know:

  1. Conceptually, the custom blocks that you are creating share a lot of common "properties". For example, all blocks describe an author and present information about the specific author.
  2. You may have only 5 (or 2, or 3) authors right now, but in the future more authors may be added. How will you add the extra authors? You will have to create new custom blocks, copy and paste the PHP code and change the information in the block to match the new author (an old programming teacher of mine used to say: when you are copying and pasting large chunks of your code, there is something wrong with your code). Not to mention that your blocks administration page will become a nightmare to manage.
  3. Even if you are absolutely sure that you will not have to add more authors in the future, are you sure that you will not want to change the information in each block? For example, if you want to remove the "Occupation" field from the block and add an "IM" field, how will you do it? Go to every block, for every author and remove and add new fields.
  4. When you'll need to change anything related to the blocks, how sure can you be that you'll remember how the PHP code works in each block?
  5. What if you want to give someone else permission to change the author's information? Perhaps, the authors would like to add their own information (e.g. add their own bio). Will you give them access to a block with PHP code?

- My approach would be significantly different:

  1. New content type: Authors. You add fields that you want to display (like: picture, bio, email, website etc)
  2. Create a new node for each author. Add the corresponding information for each author (or even give access to other users to do it).
  3. Associate the "Author" field in your existing content type with the new content type ("Authors") using References if you are in Drupal 7 or Node Reference which is a part of the CCK if you are in Drupal 6.
  4. Create a block via Views to display the information from the new content type's nodes.

P.S: I intentionally did not include the details on how to setup Views or the References module for 2 reasons: (a) I do not know which version of Drupal you are using and (b) I understand that the chances of changing your whole approach are slim. However, if you decide to follow my method, let me know and I'll edit my post to add some more details.

  • This is actually the best answer to the question overall because if you use Views to standardize each block then you can use Panels to display the same dynamic block on every node of content type X. – Jay Feb 24 '12 at 21:14

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.