1

I am experienced Java/C++ programmer, but totally new to Drupal/PHP.

Short question:

How do I refresh all the blocks in my page, based on the input to a particular block?

Exact Scenario:

I am looking to create a website with display and behaviour similar to http://www.google.com/finance. I have started creating a custom module for this in Drupal 7, So we want to have:

  1. a top input block where users can enter a particular company's name.
  2. a main block which starts with general content (e.g. tables plus latest news about the economy etc). As soon as a company is selected in block 1, this changes to news and tables about the chosen company.
  3. a side graph block displaying some relevant graphs etc. Again, when no company is chosen, this could display the general graphs (e.g. S&P, Dow Jones, NASDAQ), and when a company is chosen, this displays the details for the chosen company.

The way I see it, the website works in a "current context" for the user. So, perhaps I can set a session level variable in Drupal, and refresh all blocks based on the current value of this variable?

However, I am not sure how to achieve this, and what is the best way to do this? (AJAX? Taxonomy?)

Any pointer, hints, suggestions, examples, sample code are most welcome.

apaderno
  • 28,547
  • 16
  • 75
  • 90
vivek
  • 11
  • 3
  • actually, any Java programmer, should check out google's toolkit, much more powerful, and easier to work with than php based CMS like drupal etc. – vivek Sep 15 '12 at 22:24

1 Answers1

2

This is how I would approach this problem based on how you have described it. The majority of the functionality being handled by the Views module.

I'm assuming you have the following setup:

  • A Taxonomy vocabulary called "companies" which has company names as terms.
  • A Content Type called "News", which has news information about companies. Most importantly it will need a taxonomy field where you can select which companies it is related to (lets call this field "company_reference")
  • A Content Type called "Tables"(?). I'm not sure what information you want in your "tables", but again it's most important that is has a taxonomy field to reference companies. (can be the same field "company_reference")

The Majority of the functionality you are looking for can be built using the Views module.

I would create a View (let's call it "Company Data"). the view is going to have three different displays, each of type "block".

Display 1: Input Block

  • Set the display name to something meaningful, say "user_input_block"
  • For this block, leave FORMAT settings as they are.
  • For the FIELDS settings, just have "Content: Title" (does not really matter for this block).
  • For the FILTER CRITERIA settings, add a filter and select your "company_reference" field, set it to auto-complete, expose the filter to visitors and (under the "more" section) change its Filter Identity to "company".
  • For the PAGER settings set it to display a specific number of items and set it to '1'. (this will limit the data this block retrieves)
  • Under the Advanced section change the "Machine Name" so comething meaningful, say "user_input_block"
  • Still in the Advanced section click on "Theme: Information". this will display a list of the different custom template files you can have for this view. For "Display Output" write down the last template suggestion in the list, it will be something like "views-view--company-data--user-input-block.tpl.php" ("views-view--{your view name}--{your display name}.tpl.php"). Click on "Display Output" and copy the PHP it lists. (this is the views default PHP for the view).
  • In a text editor/IDE (whatever you use) paste the copied PHP code and save it in your custom theme with the template name you wrote down. Edit the PHP and either comment out or remove the section that says "<?php if ($rows): ?>...<?php endif; ?>" (this will remove the returned content from the display)

So to review the view display that was just created will (using the custom template) display a block with just a field that as the user is entering a company name it will autocomplete. It will then submit the form and pass it as a GET variable to the current url (www.yoursite.com/yourpage?company=users company").

Display 2: ** Main Block**

  • Set the display name to something meaningful , say "company_news".
  • Set the FORMAT settings to which ever you like (or leave as is)
  • For the FILTER CRITERIA add a "Content:type" filter and select your "News" and "Tables" content types.
  • In the Advanced section click "add" next to "Contextual Filters". )A contextual filter is passed in the URL, we are going to be grabbing the value that was passed from the Input block.) Select "Content: field_company_reference" as the field and click "Apply"
  • for the "when the Filter value is Not available" section select "provide default value"
  • for the "Type" select "PHP code" and the PHP code will be something like this "return isset($_GET['company']) ? $_GET['company']:false;"
  • for the "When the filter is available of a default is provided", check "Specify validation criteria", set "Validator" to "Taxonomy term", check the "Companies" vocabulary and for "Filter value type select "Term name converted to Term ID"
  • for "action to take if filter value does not validate" select "display all results"

To review, this view display will display a block that lists "news" and "tables" content. If the GET variable "company" is passed (from the input block) then the content of this block will be filtered to display only content that is associated to that block.

Display 3: ** Graph Block**

I'm not exactly sure what data you want to display in this block (or rather where it would be coming from) but if you set it up like how I described setting up the Main Block it will filter by company (use the same contextual filter and content that has the company taxonomy reference field).

The one difference with this approach is that it requires the page to be reloaded when a user enters a company into the input block. If you want to avoid this then you could make the following changes;

  • for the main block and graph blocks instead of using a contextual filter use a regular filter for the Company reference field (same settings as were setup in the input block). For both these blocks under the Advanced section, set "Use AJAX" to "Yes", and use custom CSS to hide the exposed filters from view.
  • with a custom module (or with your custom theme) that adds additional JS to the page. Have the JS triggered by the field in the input block, when a user enters a value into it, the JS copies that value into the exposed (but hidden) field for the other blocks, triggering Drupal to reload them via AJAX with the passed filter.

I haven't tested any of this and it is just theoretical based on my knowledge of Drupal, but it should start you out on the right path.

mmiles
  • 961
  • 6
  • 9