3

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.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Dustin
  • 4,314
  • 12
  • 53
  • 91
  • When you create a block, you get to choose the fields you want. Are you refering to how to render those fields in a custom manner? Otherwise, Drupal already lets you do it. – Deleteman Jan 05 '12 at 16:48
  • @Deleteman: You're probably thinking of content types, blocks can't have fields attached to them – Clive Jan 05 '12 at 17:31
  • Next time, please consider posting Drupal questions at drupal.stackexchange.com. – Gogowitsch Jun 30 '13 at 09:42

6 Answers6

4

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.

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
i.bajrai
  • 87
  • 5
  • Yes, but adding a form element is only part of the job. You also need to handle the storage of the entered data in hook_block_save, which is relatively easy if you're simply writing a string to the variables table, but gets more complicated when you're dealing with images. – marcvangend Jun 20 '13 at 12:33
3

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

Elijah Lynn
  • 12,272
  • 10
  • 61
  • 91
1

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.

Flex Elektro Deimling
  • 1,599
  • 1
  • 17
  • 16
1

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.

Aaron Ortega
  • 556
  • 2
  • 7
0

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>
Clive
  • 36,918
  • 8
  • 87
  • 113
  • Yeah I could put HTML in like that but I need this to be editable for people who don't know HTML. – Dustin Jan 05 '12 at 18:05
  • You'll probably have to write a custom module for that I don't know of any existing modules that do that (that's not to say there isn't one though) – Clive Jan 05 '12 at 18:29
0

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.

DuaelFr
  • 395
  • 1
  • 5