Hi,

I'm fairly new to Drupal 7 development and am looking for some help with some best practices that I may be missing.
I am currently working with a website that has a different content page for each customer. The code for each customer has a lot of common code and some small differences to pull particular customer files, etc.

When creating a new customer, we have a main code template that we just copy and modify to our current needs.
Of course this leads to a lot of code redundancy. If we needed to update something in the content code, it would have to be updated in every single customer. To avoid this, I'd like to create a common code file which could be included or something along those lines.

What methods do people use to avoid this issue of redundancy?

Thanks for the help,
Adam

Comments

John_B’s picture

The question cannot really be answered without clarification. It is not clear whether each customer has different content (in the database) outputting different HTML, or has (for some reason which is not clear) a different theme page template or theme node template or theme template for the user page; or whether you are referring to differences in backend code.

My guess - and I could be wrong of course - is that you have not yet developed a good grasp of how Drupal works, and most developers when they start with Drupal do risks building a site in an un-Drupally way which can case the client big headaches and costs down the road. It is astonishing how many Drupal projects run into the sand, and are passed to two or three developers until the client finds the right one who can complete the job. Drupal does takes a few months hard work (if you are already a good php developer - otherwise it's longer) to learn.

I am sure if you rephrase the question in a way which makes it clearer where you are at in the learning curve, and what kind of support would be useful, as well as giving a more precise idea of what it means for a customer to have different code, a more useful answer will be forthcoming. Sorry not to be more decisive!

For general stuff, see the Documentation tab at the top of this page, and consider signing up for the excellent training offered by outfits like drupalize.me and buildamodule.com, as well as checking out the books on Drupal dev.

Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors

AdamMac’s picture

Hi John,

Thanks for the reply. This website was developed by someone else and I am currently ramping up my drupal knowledge.

To clarify what I mean, each client that we have has a content page created for them.
The body of each content page holds the code for the display of the specific client. (we create an iframe that connects to a server and displays certain graphs, etc).
For each client, the iframe code, token creation for server call, etc are identical. There are just a few places where we need to change some variables to specify what client this page is for. My question comes is if there is a way to basically pull this common code out and use an include statement or something similar to save myself from redundancy and updating nightmares.

Let me know if I didn't make anything clear enough.

Thanks again,
Adam

Jaypan’s picture

The body of each content page holds the code for the display of the specific client. (we create an iframe that connects to a server and displays certain graphs, etc).

How are you creating these pages? Are you creating separate nodes for each client? Are you using a View? Is it a custom page callback?

AdamMac’s picture

To create these different pages, a new node is created for each client.
Each node is of the same type. This type has a body field (among others) which is populated with php code that I mentioned above.
This is the code which I would like to simplify/extract common methods.

Jaypan’s picture

Code in the body of nodes is pretty much isolated. It is not compiled with the rest of the page, rather after the page has been loaded, the code is then pulled and evaluated.

Many developers will actually tell you it's a big no-no to use PHP in node bodies, and in fact the functionality has been removed from Drupal 8.

I don't know that there are any ways to do what you wan when the PHP code is in the body of a node. Hopefully someone else will come along and give you a solution.

John_B’s picture

If Jaypan say says he does not know whether there are other ways - that is not ingorance, it is only because you have not said what the code is doing.

Bear in mind that php in the body of the node goes in the database. There are a whole raft of reasons for not doing this: to learn some of them, Google for something like 'Drupal php module.' Delivering a Drupal site with php module enable would in most cases be considered as delivering a site of very low quality indeed, so it would be better to look for ways to achieve your objectives which work without PHP module enabled (and without business logic in the theme files). In Drupal there are usually half a dozen ways to achieve the same thing - too many rather than too few; there is no single right answer, though are some fairly wrong answers, and several legitimate answers, to most problems.

Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors