Preamble
I began my last blog post, "My Drupal, AI, and Schema.org Manifesto," with an introduction that helped me to start discussing the use of AI in my daily Drupal work. This introduction reveals that I feel overwhelmed about where to begin exploring integrating AI with Drupal.
A Drupal AI module and ecosystem exist that have accomplished remarkable feats. I am eager to delve into the implementation and extensibility of the Drupal AI module; however, diving immediately into the code is daunting and could hinder my need to familiarize myself with using AI and comprehending how it works. Therefore, I am returning to the basics and revisiting one of my first Drupal achievements: building a module… using AI.
Hypothesis
AI will soon replace basic Drupal tasks, including creating simple modules. When this happens, the definition of entry-level development skills will evolve. AI will not eliminate jobs; instead, it will enhance workers' productivity and efficiency. Therefore, I want to explore how capable AIs are at building a Drupal module. I also want to explore if AI can help a new Drupal developer create their first module.
Theories
Below are some theories I want to explore and keep in mind.
- Keep it simple and small.
AIs can comprehend vast amounts of data and relationships - we cannot. - Provide a lot of context.
AI performs significantly better when given as much context as possible. This ensures that both people and machines understand each other. - Failure is part of the process.
Assume there will be mistakes and dead ends. An iterative approach leads to a better solution and understanding.
Goals
I've already used the word 'overwhelmed' a few times, and to keep my exploration and experimentation from going off the rails, I need a goal-oriented approach.
My Goals
- Develop a high-quality module that can be contributed back to the Drupal community.
- Include the prompts used to generate the module within the module.
- Provide a module that AI and other contributors can iteratively improve.
Build a Drupal Module
For my first Drupal module created with AI, I wanted it to be both useful and simple. Modules, when possible, should focus on one task and perform it well. Breaking applications into smaller, testable components allows both humans and machines to better understand the code. I will use the paid version of ChatGPT 4o for this experiment, but I will continue to call it the "AI" because the prompts and concepts should work with most AIs. The module is for a client who wants phone numbers in HTML to be automatically linked using the 'tel:' protocol. The client is based in the US; therefore, we will only link US phone numbers for now. Ideally, the module should be extendable to include international phone numbers. A Telephone filter module for Drupal 7 already meets these requirements, but rather than porting it to Drupal 8, let's create it from scratch. I will contribute it to the module via the Update telephone filter to Drupal 10 & 11 [#3489528] ticket. Let's start building this module using AI.
Prompt Engineering
I am entirely new to prompt engineering. My goal is to create prompts that give explicit instructions to AI. The prompts should also serve as a learning tool for a developer learning how to build their first Drupal module using AI.
The prompts will begin with general information about the module and then progress to specific features and functionalities. I will include links to reference information from Drupal.org. Ideally, the initial part of the prompts will be reusable. The prompts should also encompass all business requirements. Both the prompts and the approach will evolve over time.
Ironically, I began creating my prompts from scratch, but soon realized I should ask the AI for examples of the prompts needed to create a Drupal module. Once I saw the AI generating a list of prompts, followed by, "Would I like these prompts in different formats, including Markdown?" I recognized that one of the first rules for working with AI prompting is always to start with the AI. Additionally, the markdown suggestion for managing prompts inspired me to decide that Markdown is the ideal format for achieving my goal of documenting everything. This leads to a second rule/thought: an AI is not "creative," it can be "inspirative."
Prompt Experience
When I started writing the prompt to create the 'Telephone Filter' module, I realized that getting the perfect prompts would be a little impossible. My experience with the AI felt like I was talking to a junior developer who had read every book, article, and blog post about Drupal, and they were overwhelmed when it came to stitching everything together properly into a module. As a senior developer, I broke down my requirements into general guidance for creating a Drupal module, followed by specific details about the 'Telephone Filter' module.
My workflow involved repeatedly cutting and pasting my Markdown requirements into the AI and reviewing the output in the sidebar. I admitted that I got so obsessed with these Markdown requirements and I tested and tweaked them repeatedly for several days, and the output from each experiment was different.
The best analogy I can think of for the overall experience of prompting an AI to build a Drupal module based on my specifications is that of a director guiding the cast and crew to create a production.
The similarities between prompting and directing are:
- You must have a vision and set reasonable expectations.
- Accept that it takes multiple attempts, and each attempt yields different results.
- Occasionally, you need to nudge things in the right direction, while conversely, you need to let things go in unexpected directions.
- Always be open to new ideas and learning new things.
Prompt Markdown and Demo
Below is the prompt Markdown I used to generate the Telephone Filter module for Drupal 10+.
Here's a quick video of AI interpreting my " Build me a Telephone Filter module" prompts to achieve the outcome: a downloadable and contributable Drupal module.
The AI got me 80% of the way there, and then I had to spend 30 minutes refining, fixing, and tweaking the code. Still, it was a good starting point, and it will only improve from here.
Lessons Learned
A few lessons learned about using AIs
- AI prompting is an iterative process
Although I provided all the guidance via Markdown at once, the AI required additional prompting to build the entire working module. - Provide some examples
Adding examples of different phone numbers ensured the AI knew what to look for. - Tiny little directives can go a long way.
Adding directives such as 'Do not use abbreviations for variable names' and 'Use camelCase for all variable names' will help AI implement the desired naming conventions. - AIs can't copy external code.
This is due to copyright restrictions. If you need a specific template, in my case, the .gitlab-ci.yml template had to be included in the instructions. - Allow the AI to figure things out.
Providing guidance on the types of tests you need is helpful, but let the AI determine the ideal test data.v
Conclusions
As a senior developer, I see endless potential in working with AI. The results and process can be a bit murky for a junior developer trying to learn how to build their first Drupal module using AI. While the AI possesses a wealth of knowledge about Drupal, it struggles to stitch all the information together or explain it clearly. Using AI to review a module's requirements would help me mentor a junior developer because it requires me to be explicit in my requirements.
For the Drupal community, we need to embrace the potential of AI for both experienced and inexperienced Drupal developers. The latest episode of Talking Drupal focuses on Getting Back to Drupal Basics and helped inspire this blog post's tagline. Our community should introduce and encourage entry-level "basic" Drupal sessions at conferences and camps, and maybe we can also incorporate AI tips and tricks to inspire current Drupal developers and welcome newcomers.
We should seek to inspire and welcome developers because "the drop is always moving," and AI is pushing the drop in a new direction.
References
Here are some articles I read and videos I watched for inspiration for this blog post.
- Prompt Engineering, Generative AI, and Drupal
- Drupal Module Development with AI
- AI-Powered Custom Drupal Module Development
- https://drupalize.me/blog/learning-drupal-help-ai-tutor
- How Close is AI to Acting As a Drupal Developer?
- A collection of prompts for generating high quality code...
- Helpful 201+ Best ChatGPT Prompts for Coding