Can Claude help us with the issue queue?

Can Claude help us with the issue queue?

Hand using a stylus on a laptop with digital AI graphics overlay.

I have many part time jobs and one of them is teaching "Developing open-sourced web software with Drupal" at Reykjavík University in Iceland. In my class the students will create a musical database website, with artists, album and songs.

One of the field requirements for albums/songs was to have a text field with the Spotify ID. That requirement was based on the fact that when I started teaching this course, five years ago, a module existed that allowed use to create a Media entity for Spotify elements. That module then had a Field Formatter that rendered the spotify link as a iframe widget with a play button. Pretty nifty module.

The Problem? It was last updated in 2017, leaving it vastly outdated. And since it was developed for Media Entity module, not for the Media module, the methods to create the plugin was very different from how we do things today.

Of course, it would be awesome if I could just ask the students to update the module as a part of the course, but my experience is that until they have created their first project by themselves, they can from there understand the mistakes they made in that project and can move on and start maintaining other peoples' code.

So how about that AI that I've been hearing so much about?

I've tried to use ChatGPT, Copilot and Gemini a bit to help me with regular text and to answer questions, but very often I've found that the answers are pretty much just guesses or fabricated answers. For an example, I asked ChatGPT what the newest version of Drupal was and the reply was 10! Many other questions regarding many things had resulted in ... mixed results at best, although sometimes it was right on the nose.

But I did hear about Claude at DrupalCon Vienna so I thought to myself: Hey, let's be open-minded about this "new stuff" and try it out. So I created a new project with DDEV, installed Claude and downloaded the module with git (since composer rightfully declined fetching it, due to multiple constraint violations).

The direction I gave was: "I have a module under custom called media_entity_spotify. It was created in Drupal 8 when Media module was called Media Entity and was contribution module. Is there a way to refactor this module to create a Media element called Media Spotify and use the field formatter provided to display Spotify links with the player?"

Claude started asking me permissions to open files and edit others, which, in the end, I gave just full permission to alter, because it was stopping too often :-)

After the first go, I was able to install the module without any problems. The Media entity was available for creation so I went ahead and created it. And then the first fatal error occurred. So I prompted it again, explaining my problems:

"When calling /admin/structure/media/add I get this error: Fatal error: Type of Drupal\media_entity_spotify\Plugin\media\Source\Spotify::$configFactory must not be defined (as in class Drupal\media\MediaSourceBase) in /var/www/html/web/modules/custom/media_entity_spotify/src/Plugin/media/Source/Spotify.php on line 30". Again, Claud went "researching" and realized the error it did and fixed it. I made a slight error when I was inserting the URL (assumtion on my behalf) but the first test, adding a song worked great. The media entity was rendered via the Field Formatter and the song played through Spotify.

My next try didn't go so well. I tried entering a playlist and I got notifications on the screen because I was trying to use native PHP functions by assigning NULL values, when expecting arrays. That made me find two errors, first, playlists weren't parsed correctly and also the check wasn't defensive enough in how to react if no data came through the Media parser. My prompt to Claude was: "If I create a media item with this link https://open.spotify.com/track/4tozzHVS3vnc8xks2PekDr the module renders one song and in the Formatter on line 203 the $uri variable gets its value. But if I have a media item with a playlist, like this one https://open.spotify.com/playlist/0bXZMCsiOoESIuMBXU45gX, nothing gets rendered and $uri variable gets a null value. Can you figure out why?"

And surely it did figure out why. The reason was that Spotify had changed the URLs of their playlists and the module was still using the old version. Claude didn't (obviously) not check to see if the API calls were functioning correctly towards Spotify. Simple fix this time.

The last problem I found had to do with thumbnails: "I found out that thumbnails are not displaying properly for songs and playlists. Nothing is displayed in the Thumbnail column. But I do see that some kind of files are saved in the spotify directory of the filesystem..."  And that had to do with using file_exists() function and public:// stream wrappers. That solution was fixed with $this->fileSystem->realPath().

And that was it! The refactoring was successful, it switched from old Annotations to the newer PHP Attributes, deprecated function calls were eliminated, better error handling and code quality and now backward compatibility is an option! It took me about two hours of fiddling and basic testing.

And what about the quality of the work?

I'm not a master code reviewer, but from what I can judge, the quality is quite good. I compared the implementation of the Spotify Media plugin to other plugins and there is nothing that poked my eye. And the formatter is correctly implemented, but then again, it was converting old code, not building new one and the Field Formatter implementation wasn't that different from the original. I'm actually thinking of submitting this code to the module's repository to get a better code review, confirming if this refactoring was as successful as I think it is.

At least I'm very intrigued to continue down this path of exploration and maybe, one day, we can have Claude (and other AI tools) help us with maintaining our code repository, when we are transforming code between versions.

Where can I find the code?

The original code of the module can be found here: https://www.drupal.org/project/media_entity_spotify 

The converted code is still residing on my personal Github, until I decide if I'm going to upload it or not: https://github.com/drupalviking/media_entity_spotifyhttps://github.com/drupalviking/media_entity_spotify

Get ready to transform your operations

Sign up for a free trial and discover a world of possibilities to elevate your outreach and engagement strategies