Question

I'd like to be able to add custom fields to blocks in Drupal? I have a header area that spans the entire width of the page. Below that will be a left column for body content and right sidebar for various blocks. The header area needs to have a background image with text displayed on top of it. Was hoping to do this through blocks with custom fields for the background image and text.

Was it helpful?

Solution

You could achieve this using the Views module, Fields and blocks.

  1. Create a content type called Background Image with an Image Upload field and any other fields you'd like to display
  2. Using Views create a View that displays as a block. You can set up the fields to rewrite however you'd like.
  3. Using your Views settings, display your background image as a path rather than an image.

For instance you could set up something like this using field replacements in views:

<div style="background: [field_image] left top no-repeat">
<h2>[title]</h2>
<p>[field_whatever]</p>
</div>

It seems like a bit of overkill, but it would get it in the hands of the client. You'd also want to do something restrictions to keep it only using a certain node ID so that they don't create a million background image nodes and wreck the site. Hope this helps.

OTHER TIPS

The better way would be to make a custom block in your code using hook_block_info() and hook_pages_block_view(). Then add the additional fields via hook_block_configure().

Using the form api you can then add any type of field you want.

Use the Bean module, this will allow you to create custom Block types with fields, even image fields. It integrates nicely with the blocks module. Watch this video for a demo - http://www.youtube.com/watch?v=Eu1YNy-BNG8

The way I do it, (and i know of several others who do it that way too ) is to make a content-type "block content" for example. There you can create as many fields as you wish.

Then you create a node-reference - field at the node-type you whish where your blocks (mynodetype) shall appear.

Then you make a views - block : i.e. "custom block" where you give a relationship - context. Mostly this context will be something like : Show fields of the node (nodetype : block content) which is referenced by the reference field in "mynodetype" .

I found this a quite clean solution and it works good, because you have just one block which is shown when needed, an the content is still in nodes and doesn't pollute the blocks admin page.

You can only attach fields to entities, since blocks are not entities I'm afraid you can't solve your problem with fields.

Your best bet would be to set the content of the block to the text you want to display, and use CSS to target the containing div and put the background image on that.

Alternatively just change the filter for the block content to 'Full HTML' and write your styles inline, eg.

<div style="background:url(some/path)">Block content here</div>

You could try to use Node Blocks to define a content type which will automatically create blocks. Then, with a bit of CSS you will be able to do what you want to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top