I'm working on a bit of Drupal 7 theming, generally modifying a few fields, nothing to complex.One thing that is frustrating me is I keep repeating 80% of the same code in each function as shown below for a particular content type.

What is the best practice, how do I combine this code/functions so I don't DRY ?The only things different are the function names and the first span class.

The only actual difference between the two functions seems to be the class name that's applied to the span element...

At the moment yes, though I need the two different function names as these function names are unique to the drupal fields, I also have a third function which also has the same code excluding the <span>. So in total we have:

you'd basically want to make it generic so that it applies to any event type, with the type passed as the first parameter

And could you elaborate on this.

FYI

theme_1 = the name of our themefield = we're targeting a field (can be other things like, block or view)field_event_location = the unique field nameevent = the content type name

Putting all that together = theme1_field__field_event_location__event

oddz
—
2014-06-05T22:55:03Z —
#9

You would just call the function which you copied the code from. If the file that function exists in hasn't been included yet than you would need to manually call drupal_load_include to bring in the file.

It works fretburner I see what you have done here, this is exactly the type of approach I was referring to.

Likewise with oddz suggestion which I tried first though the <span> elements were being printed outside of the <li>, not sure if I was doing it correctly.I'll digest all this properly tomorrow and get back with updates, very tired now (:

Also hoping to see what oddz suggests once I explain a little further, maybe we can fine tune this even more

Thanks again all for your time.

Chat soon, Barry

computerbarry
—
2014-06-08T00:04:04Z —
#12

Though there is likely a *better way to achieve your end goal than doing that. I would have to understand the whole scope to actually recommend anything else.

I'm general just changing/adding a bit of markup, new attributes to some fields inside my event content type.

The only other alternative I could see oddz was to create a load of template files field--field-event-date-and-time.tpl.php and so on.I was under the impression that if possible, its better to make the changes inside your template.php, faster ?

What other ways are there?I'll soon have more fields I need to change, things can soon add up.Is there a best approach, wouldn't my template.php soon become heavy with code for making tweaks to fields, or would I be better creating template files?

Nice to hear you view on this and how you do things yourself.

And thanks again fretburner, currently using your method.

Thanks,Barry

oddz
—
2014-06-08T00:46:54Z —
#13

Why do you need to add additional attributes and change the markup in the first place.

computerbarry
—
2014-06-08T11:09:48Z —
#14

So I can customise my site more. Sometimes I'll need to add spans and other html elements, custom graphics, microdata, data-attributes amongst other things.Isn't this what the theming layer is for so we can build our own theme to how we like it?

So whats best, template.php or .tpl.php ?A mixture or do you recommend a different approach oddz?And are you saying what I'm doing above is wrong?

I just want to make sure I'm doing things correctly before I dive into a big build.Appreciate your feedback thanks.

Cheers, Barry

oddz
—
2014-06-09T02:43:57Z —
#15

It's difficult to speak on a generic level about this topic. Could you provide some concrete examples of what your actually trying to accomplish from a design standpoint.

computerbarry
—
2014-06-09T10:28:48Z —
#16

In a nutshell oddz,I'd just like to understand the best way of modifying fields, best practice, writing less code and speed.Generally, how to add extra markup and attributes to fields when I need extra customisation per theme for individual fields.

Cheers, Barry

oddz
—
2014-06-09T22:04:33Z —
#17

Well to be honest I've never needed to override the HTML output of a field provided by core or a contrib module. Having said that I have created my own custom fields, and used token replacement in views to customize HTML. I've also created custom view modes and displays for fields to solve other problems. There are many different ways to achieve things in Drupal. Though on a generic level it is difficult to recommend any one approach. Typically though if you have to repeat code like you have done there is a better means of solving the problem at hand. Typically… not always.

computerbarry
—
2014-06-09T22:22:12Z —
#18

There are many different ways to achieve things in Drupal.

As I'm finding out

I have created my own custom fields, and used token replacement in views to customize HTML

I have come across tokens though haven't really used them yet, generally been learning and working with code, rather than the admin.

Though on a generic level it is difficult to recommend any one approach

Might sound like a silly question, but what exactly do you mean when you say 'generic level' slightly confusing me, what does it mean?

Thanks, Barry

oddz
—
2014-06-09T22:39:49Z —
#19

I mean why do you need to add attributes or classes. Do you need them as hooks for other libraries to apply css or javascript perhaps.