How to add class on views table row (Drupal 7)

For a views using table, if you want to add a custom class on <tr>, you can go to this view’s setting page, find the ‘Foramt’ section, click the ‘table’s ‘setting’.

Screen Shot 2015-04-24 at 12.09.35 pm

 

Then find ‘Row class’ field. Just add your class here. But if you want a dynamic class, you can use field tokens. Screen Shot 2015-04-24 at 12.08.51 pm

You don’t know what is the field token. But don’t worry, you can find the field tokens in here, click the field in the ‘Fields’ section.

Screen Shot 2015-04-24 at 12.12.28 pm

But if the field is not there, you can add the filed and tick “exclude from display”. Ignore the error message please 😀

Screen Shot 2015-04-24 at 5.02.51 pm

Then find the ‘Rewrite results’:

Screen Shot 2015-04-24 at 12.22.23 pm

Save and done.

 

 

 

How to add block in module (Drupal 7)

Using hook_block_info(), here is example: https://api.drupal.org/api/examples/block_example!block_example.module/7

A simple code example here(copied from here):

But all above is missing a lot details, like module configuration. Here is the details of hook_block_info block array, copied from Durpal docs:


 

An associative array whose keys define the delta for each block and whose values contain the block descriptions. Each block description is itself an associative array, with the following key-value pairs:

  • info: (required) The human-readable administrative name of the block. This is used to identify the block on administration screens, and is not displayed to non-administrative users.
  • cache: (optional) A bitmask describing what kind of caching is appropriate for the block. Drupal provides the following bitmask constants for defining cache granularity:
  • properties: (optional) Array of additional metadata to add to the block. Common properties include:
    • administrative: Boolean that categorizes this block as usable in an administrative context. This might include blocks that help an administrator approve/deny comments, or view recently created user accounts.
  • weight: (optional) Initial value for the ordering weight of this block. Most modules do not provide an initial value, and any value provided can be modified by a user on the block configuration screen.
  • status: (optional) Initial value for block enabled status. (1 = enabled, 0 = disabled). Most modules do not provide an initial value, and any value provided can be modified by a user on the block configuration screen.
  • region: (optional) Initial value for theme region within which this block is set. Most modules do not provide an initial value, and any value provided can be modified by a user on the block configuration screen. Note: If you set a region that isn’t available in the currently enabled theme, the block will be disabled.
  • visibility: (optional) Initial value for the visibility flag, which tells how to interpret the ‘pages’ value. Possible values are:
    • BLOCK_VISIBILITY_NOTLISTED: Show on all pages except listed pages. ‘pages’ lists the paths where the block should not be shown.
    • BLOCK_VISIBILITY_LISTED: Show only on listed pages. ‘pages’ lists the paths where the block should be shown.
    • BLOCK_VISIBILITY_PHP: Use custom PHP code to determine visibility. ‘pages’ gives the PHP code to use.

    Most modules do not provide an initial value for ‘visibility’ or ‘pages’, and any value provided can be modified by a user on the block configuration screen.

  • pages: (optional) See ‘visibility’ above. A string that contains one or more page paths separated by “\n”, “\r”, or “\r\n” when ‘visibility’ is set to BLOCK_VISIBILITY_NOTLISTED or BLOCK_VISIBILITY_LISTED (example: “<front>\nnode/1”), or custom PHP code when ‘visibility’ is set to BLOCK_VISIBILITY_PHP. Paths may use ‘*’ as a wildcard (matching any number of characters); ‘<front>’ designates the site’s front page. For BLOCK_VISIBILITY_PHP, the PHP code’s return value should be TRUE if the block is to be made visible or FALSE if the block should not be visible.