+++ title = "When There's Not a Module For That" outputs = ["Reveal"] +++ # When There's Not a Module For That ###### [mid.camp/6337](https://mid.camp/6337) Agaric {{% note %}} mid-air camp {{% /note %}} --- ###### Presented by **Benjamin Melançon** & **Mauricio Dinarte** ###### AKA **[mlncn](https://agaric.coop/mlncn)** & **[dinarcon](https://agaric.coop/dinarcon)** --- ### Together, ## We are #### ⅓ #### of --- Agaric **ask@agaric.coop** --- Agaric {{% note %}} mid-air camp {{% /note %}} --- > When building a Drupal site, “there’s a module for that” can be the sweetest words you can hear. {{% note %}} {{% /note %}} --- What do you do ## When there's not a module for that ? {{% note %}} You make your own. It's easy. {{% /note %}} --- ![Regular node add form with Save button](images/create-frankenstein-unmodified.png) {{% note %}} Say you're creating a site where people can relive great literature that may have been inspired by a famine. You've already created an excellent content type under Structure » Content. {{% /note %}} --- `frankenstein.info.yml` ```yaml name: Frankenstein type: module description: "Rename save button for Frankenstein's content." core: 8.x|9.x ```   `frankenstein.module` ```php {{% note %}} The examples with the what will tell you a lot about the how. A few specific examples will give you a lot of tools. But first, two secrets. One's good news, second is bad news. {{% /note %}} --- ### 1. Now you know where to paste ![How do I exit the Vim editor? viewed 2.1 million times](images/stack-overflow-helping-devs-quit-vim.png) {{% note %}} And knowing where to paste unleashes the power of Stack Overflow / Stack Exchange sites. (yes, the same family of sites that has helped a couple million developers figure out how to quit vim.) Knowing where to put this code unleashes the power of Stack Overflow. You now know enough to be dangerous. {{% /note %}} --- ### 2. That simple form alter has hidden gotchas   * As written, it only applies to the create (node/add) form— not the edit form. * There's a dozen variations of the humble form alter hook, and all are valid. {{% note %}} With our dirty secrets acknowledged, on with the show. {{% /note %}} --- 1. Act One: How Not To Make a Module 2. Act Two: How Not To Have to Make a Whole Module 3. Act Three: How To Make a Module 4. Act Four: How To Figure Out How To Make a Module --- ## We'll cover * Hooks * Form alter * Node insert * Plugins * Block * Formatter * Services * Route subscriber * Event subscriber {{% note %}} the three main ways to mess with Drupal 8 and 9: hooks, plugins, and services, with a couple examples in each. both examples of what technically to do and how to do general things For example: Doing something with a form? You want hook_form_alter() or one of its variants. {{% /note %}} --- ## Learning to learn * Drupal documentation * Drupal contributed modules * Drupal core and *its* modules * Drupal forums and issues * Drupal.StackExchange.com * The rest of the Internet * Debugger * Code search / grep {{% note %}} Where to start if i don't cover it in this session {{% /note %}} --- #### When not to make a module? [![Turtle with golf ball on spring contraption on back.](images/rube-goldberg-tortoise.jpg)](https://www.koreus.com/video/machine-rube-goldberg-lente.html) {{% note %}} You can do anything with a module! Why wouldn't you make one every day? {{% /note %}} --- #### …when there's an easier way ![Ginger Howard completing her swing after hitting a golf ball](images/ginger-howard-golf-swing.jpg) {{% note %}} find a contrib module that does it Should it be in a template {{% /note %}} --- ##### Thre's a module that *almost* does exactly what you want * Contribute a patch! {{% note %}} Even if your patch is not accepted nor is there any way given to build on the module, you can fork the module— maintain your own copy. {{% /note %}} --- ### When to make a module ![Lego heart with a missing piece](images/lego-heart-missing-piece.jpg) {{% note %}} You've identified to the best of your ability that there's definitely not something out there that does what you want to do. I personally do not advocate introspection at this point: Why am i trying to do something that no one else is trying to do? Credit: lego heart is credited to Llama-Muffin-Kelly on DeviantArt but only see it on Pinterest {{% /note %}} --- #### Step 1: Write up what you plan to do [groups.drupal.org/contributed-module-ideas](https://groups.drupal.org/contributed-module-ideas) {{% note %}} The module does not exist. {{% /note %}} --- ### The Rest of the Internet * Duck Duck Go (!g for Google results) * [ddg.co](https://duckduckgo.com/) * [agaric.coop/blog](https://agaric.coop/blog) * Your own notes {{% note %}} {{% /note %}} --- --- ## Why contribute? * Right thing to do * I asked you to * People may contribute to it * Someone may take it over and upgrade it or improve it greatly {{% note %}} {{% /note %}} --- [![Workflow buttons issue: Add a configure link in Extend page.](images/workflow-buttons-add-configure-link-in-extend-page.png)](https://www.drupal.org/project/workflow_buttons/issues/3120524) {{% note %}} Or someone may make a tiny improvement that makes everyone's lives better. {{% /note %}} --- ## Nice touches `workflow_buttons.info.yml` ```diff type: module description: 'Provide workflow buttons for content moderation instead of a select dropdown of states.' core: 8.x +configure: workflow_buttons.settings dependencies: - drupal:system (>=8.4) ``` {{% note %}} {{% /note %}} --- ### How to develop while sharing the same code you're using Your module `composer.json`: ```json { "name": "drupal/cyborgtranslate", "description": "Trigger Google translate and Drupal interface translation at the same time, allowing sections of your site's localization to be done by humans and the rest by machines.", "keywords": ["drupal", "translation", "multilingual", "localization", "google translate"], "type": "drupal-module", "license": "GPL-2.0+", "homepage": "https://www.drupal.org/project/cyborgtranslate", "minimum-stability": "dev", "support": { "issues": "https://www.drupal.org/project/issues/cyborgtranslate", "source": "https://gitlab.com/agaric/drupal/cyborgtranslate/tree/8.x-1.x" } } ``` {{% note %}} Composer makes a lot of stuff seem more complicated, but it makes it easier to contribute. Trust me... this will only be three screens of gibberish. {{% /note %}} --- Your project root `composer.json`: ```json { "require": { "drupal/core": "^8.8.0", "drupal/cyborgtranslate": "dev-8.x-1.x as 1.x-dev", "drush/drush": "^9.0" }, "repositories": { "drupal": { "type": "composer", "url": "https://packages.drupal.org/8" }, "drupal/cyborgtranslate": { "type": "git", "url": "git@gitlab.com:agaric/drupal/cyborgtranslate.git" } } } ``` --- {{% note %}} {{% /note %}} --- ![Little rabbit in big grass](images/little-rabbit-in-big-grass.jpg) [MID.CAMP/6337](https://mid.camp/6337) · ASK@AGARIC.COOP {{% note %}} It's a big wild world out there. Bite off a piece you can chew. Links for all of this and more on the session page. {{% /note %}}