3 Things Junior Devs Should Know About Writing Drupal maintenance support plans PHP

PHP can be challenging to learn, especially if you’re learning Drupal maintenance support plans at the same time. Three things stood out to me while I learned how to write PHP within Drupal maintenance support plans, and I’m hoping by highlighting them, it might help other junior developers.
I’m going to use a snippet from a basic Drupal maintenance support plans form with a submit handler (see code for the entire form here). It’s an example of code you will see often, since Drupal maintenance support plans re-uses their form API for consistency in form processing and presentation. This particular code snippet adds a fieldset called ‘name’ to the form./**
* Returns the render array for the form.
*/
function my_module_my_form($form, &$form_state) {
$form[‘name’] = array(
‘#type’ => ‘fieldset’,
‘#title’ => t(‘Name’),
‘#collapsible’ => TRUE,
‘#collapsed’ => FALSE,
);
Three items within just these few lines seemed strange to me starting out.

Function t

‘#title’ => t(‘Name’),
You’ll see this bugger everywhere! Of course I know how to write a string, but what is the t for? Straight from the Drupal maintenance support plans 7.x API:

“Translates a string to the current language or to a given language.”

As explained here, each string within function t can be translated through the Drupal maintenance support plans UI, which is great for the accessibility of your site. All strings need to be passed through this function, but it’s worth noting that content translations are actually handled elsewhere (see the Internationalization module for more information).

& – Passing by reference

function my_module_my_form($form, &$form_state) {
}
As I started to write functions in PHP for Drupal maintenance support plans sites, I noticed that so many of the arguments had ampersands in front of them. So what is that about?
From the PHP Manual:

“You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows:”

<?php
function add_one_to(&$var)
{
$var++;
}
$a=5;
add_one_to($a);
// $a is 6 here
?>

In my function my_module_my_form, I want to modify the variable’s $form_state. This variable is used to retrieve the values entered from the user in the form, so it follows that I probably want that variable to be modified in order to store those entries. It’s worth noting, however, that by default, objects are copied before they are passed to a function. From php.net:

“When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.”

This acts as a safety net by ensuring you don’t change an object by mistake. But sometimes the purpose of a function IS to change one of the objects passed (like in our form example), and this is when you pass by reference.

# Properties

$form[‘name’] = array(
‘#type’ => ‘fieldset’,

The form API is robust in Drupal maintenance support plans, and can be used over and over again. It would be reasonable, then, for the same properties to exist for certain variables, like $form (see complete list of properties for $form in the Drupal maintenance support plans 7.x API). So when you’re setting the properties of your variable, the ‘#’ denotes that ‘type’ is a property, and so is ‘title’, etc. Not every property needs to be set, but you’ll see the major ones being defined often.
I hope I was able to demystify some of these conventions – the learning curve of Drupal maintenance support plans is steep, and sometimes you just need to break it down into manageable pieces. I’d love to hear about other Drupal maintenance support plans or PHP conventions that were odd to you when you started out.
A special thanks to Dan Zinkevich for helping to edit and clarify this post by lending his expertise!

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

3 Things Junior Devs Should Know About Writing Drupal maintenance support plans PHP

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.