Drupal at your fingertips
Free!
With Membership
$15.00
Minimum paid price

Drupal at your fingertips

A Modern Drupal developer's quick code reference

About the Book

This book is a quick reference for developers creating Drupal sites. While working on such sites during my career, I have gathered a large collection of notes and code which I use for my reference. I hope it will be useful for you, too. I've provided tested code samples for almost every topic. My goal is to save you having to dig through lots of search results to find the code snippet you need to get your job done.

  • Share this book

  • Categories

    • Computers and Programming
    • PHP
    • MySQL
    • Web Development
    • Software Engineering
    • HTML
    • JavaScript
    • Software
    • Automated Software Testing
  • Feedback

    Email the Author(s)

About the Author

Selwyn Polit
Selwyn Polit

Selwyn has over two decades' experience building and designing software applications. He is a passionate proponent of open source development, and has been working with Drupal since version 4.7. Currently focused on back-end Drupal development for progressive, mission-driven organizations and businesses, Selwyn has worked with many software languages and environments in both the for-profit and non-profit worlds, including PHP, MySQL, Javascript, Oracle, and Linux. Originally from Cape Town, South Africa, Selwyn lives with his son in Austin, TX. Selwyn's other areas of expertise include electrical engineering, psychology, Permaculture, and building Indonesian- and Japanese-style tea houses. You can find Selwyn on LinkedIn, and as @selwynpolit on Twitter and Drupal.org.

Table of Contents

    • 1:Introduction
    • 2:Actions
      • 2.1:Overview
      • 2.2:Custom Actions
      • 2.3:Archive Node Action (simple)
      • 2.4:Update Node Title Custom Action Plugin with Configuration
      • 2.5:Reference
    • 3:Blocks
      • 3.1:Create a block with Drush generate
      • 3.2:Anatomy of a custom block with dependency injection
      • 3.3:Create a block with an entityQuery
      • 3.4:Create a Block with a corresponding config form
        • 3.4.1:The config form definition
        • 3.4.2:The routing.yml file
        • 3.4.3:The Block definition
      • 3.5:Modify a block with hook_block_view_alter or hook_block_build_alter
      • 3.6:Disable caching in a block
      • 3.7:Add a configuration form to your block
      • 3.8:Block display not updating after changing block content
      • 3.9:Block Permission (blockAccess)
        • 3.9.1:Blocks shouldn’t talk to the router, NodeRouteContext and friends should
        • 3.9.2:Values returned by blockAccess()
    • 4:Batch Processing and the Drupal Queue System
      • 4.1:Batch Processing Using the Batch API
        • 4.1.1:Overview
        • 4.1.2:Using the Batch API with a form
        • 4.1.3:Using the Batch API from a controller
        • 4.1.4:Using the Batch API with hook_update
        • 4.1.5:Important rules about functions when using Batch API
        • 4.1.6:Looking at the source code for the Batch API
        • 4.1.7:Passing parameters to the functions in a batch operation
        • 4.1.8:Finishing a batch
        • 4.1.9:Showing the progress of a batch
        • 4.1.10:Stopping a batch dynamically
      • 4.2:Queue System
      • 4.3:Resources
    • 5:Caching and cache tags
      • 5.1:How to uncache a particular page or node
      • 5.2:Disable caching on a route
      • 5.3:Don’t cache data returned from a controller
      • 5.4:Disable caching for a content type
      • 5.5:Considering caching when retrieving query, get or post parameters
      • 5.6:Debugging Cache tags
      • 5.7:Using cache tags
        • 5.7.1:Invalidate a specific node
      • 5.8:Setting cache keys in a block
      • 5.9:Getting Cache Tags and Contexts for a block
      • 5.10:Caching REST Resources
      • 5.11:Caching in an API class wrapper
      • 5.12:Caching in a .module file
      • 5.13:Logic for caching render arrays
      • 5.14:Caching your data for better performance
        • 5.14.1:Invalidating caches
        • 5.14.2:Specify custom cache bins
      • 5.15:Caching data so it doesn’t get cleared by a cache rebuild
      • 5.16:Development Setup
        • 5.16.1:Disable caching and enable TWIG debugging
        • 5.16.2:Disable Cache for development
      • 5.17:How to specify the cache backend
        • 5.17.1:class ChainedFastBackend
        • 5.17.2:APCu
      • 5.18:Reference
    • 6:Composer, Updates and Patches
      • 6.1:Creating a local patch to a contrib module
      • 6.2:Patch modules using patches on Drupal.org
        • 6.2.1:Step by step
      • 6.3:Patches from a Gitlab merge request
      • 6.4:composer.json patches in separate file
      • 6.5:Stop files being overwritten during composer operations
      • 6.6:Updating Drupal Core
      • 6.7:What are the dependencies?
      • 6.8:Test composer (dry run)
      • 6.9:Add a module that isn’t currently supported in your version of drupal
        • 6.9.1:For Drupal 9
        • 6.9.2:For Drupal 10:
      • 6.10:Version constraints
      • 6.11:Allowing multiple versions
      • 6.12:Specify a particular version of PHP
      • 6.13:Troubleshooting
        • 6.13.1:Composer won't update Drupal core
        • 6.13.2:The big reset button
      • 6.14:Reference
    • 7:Configuration and Settings
      • 7.1:Load some config
      • 7.2:Views
      • 7.3:Add config to an existing module
      • 7.4:Import something you changed in your module
      • 7.5:Config Storage in the database
      • 7.6:Add some config to site config form
      • 7.7:Override config in settings.php
      • 7.8:Setup a testing variable in config for a project
      • 7.9:Getting and setting configuration with drush
      • 7.10:Creating a module allowing users to edit/update some config
      • 7.11:Drush config commands
        • 7.11.1:View config
        • 7.11.2:Viewing overridden config values
        • 7.11.3:Delete from config
        • 7.11.4:Check what has changed with config:status
        • 7.11.5:Export entire config
        • 7.11.6:Import config changes
    • 8:CRON
      • 8.1:Overview
      • 8.2:How does it work?
      • 8.3:Enable Drupal Cron
      • 8.4:The cron command
      • 8.5:Setting up cron
      • 8.6:Disable Drupal cron
      • 8.7:hook_cron()
      • 8.8:Common inquiries regarding cron jobs
        • 8.8.1:When did the cron job last run?
        • 8.8.2:How to stop Cron from continuously executing things?
        • 8.8.3:Resolving the ip and name for cron
      • 8.9:Resources:
    • 9:Dates and Times
      • 9.1:Overview
      • 9.2:Retrieve a date field
      • 9.3:Retrieve date range field
      • 9.4:Formatting date range fields
      • 9.5:Formatting a date string with an embedded timezone
      • 9.6:Formatting a date range for display
      • 9.7:Saving date fields
      • 9.8:Create DrupalDateTime objects
        • 9.8.1:Create DrupalDateTime objects with timezones
      • 9.9:Create a DrupalDateTime object and display as a year only
      • 9.10:Formatting node created time with Drupal date.formatter service
      • 9.11:Date arithmetic example 1
      • 9.12:Date arithmetic example 2
      • 9.13:Comparing DrupalDateTime values
      • 9.14:Comparing dates (without comparing times)
      • 9.15:Comparing Dates to see if a node has expired
      • 9.16:Node creation and changed dates
      • 9.17:Query the creation date using entityQuery
      • 9.18:Query a date field with no time
      • 9.19:Query a date field with a time
      • 9.20:Smart Date
        • 9.20.1:Smart date: Load and format
        • 9.20.2:Smart date: all-day
        • 9.20.3:Smart date: Range of values
      • 9.21:Reference
        • 9.21.1:Date field storage
        • 9.21.2:DrupalDateTime API reference
        • 9.21.3:UTC
        • 9.21.4:Unix epoch timestamps
        • 9.21.5:Links
    • 10:Debugging
      • 10.1:Overview
      • 10.2:Enable error reporting
      • 10.3:Disable caches and enable Twig debugging
      • 10.4:Enable/Disable Xdebug
      • 10.5:Xdebug Port
      • 10.6:Drupal code debugging
      • 10.7:Command line or drush debugging
      • 10.8:Add a breakpoint in code
      • 10.9:Troubleshooting Xdebug with DDEV
        • 10.9.1:Could not connect to debugging client
        • 10.9.2:PhpStorm refuses to debug
          • 10.9.2.1:Curl
          • 10.9.2.2:Logs
          • 10.9.2.3:Telnet
          • 10.9.2.4:Is Xdebug enabled?
      • 10.10:What is listening on the debug port?
      • 10.11:Enable twig debugging output in source
      • 10.12:Devel and Devel Kint Extras
        • 10.12.1:Setup
        • 10.12.2:Add kint to a custom module
        • 10.12.3:Dump variables in a TWIG template
        • 10.12.4:Kint::dump
        • 10.12.5:Set max levels to avoid running out of memory
      • 10.13:Resources
    • 11:Development
      • 11.1:Local Drupal site setup
          • 11.1.0.1:First Option
          • 11.1.0.2:Second Option
      • 11.2:Checking Your Permissions
      • 11.3:Converting existing site (non-composer based) to use composer
      • 11.4:Composer best practices for Drupal 8
      • 11.5:DDEV
        • 11.5.1:Local config - your .ddev/config.local.yaml
          • 11.5.1.1:NFS
          • 11.5.1.2:Mutagen
        • 11.5.2:setup aliases in ddev
        • 11.5.3:Upgrading ddev
        • 11.5.4:Show others your ddev local site
        • 11.5.5:Email Capture and Review
        • 11.5.6:DDEV and Xdebug
        • 11.5.7:Command line or drush debugging
        • 11.5.8:Use drush commands in your shell with DDEV
        • 11.5.9:Load your data from an Acquia site
        • 11.5.10:Cleanup some disk space
        • 11.5.11:Accessing specific containers
      • 11.6:DDEV Troubleshooting
        • 11.6.1:Running out of docker disk space
        • 11.6.2:DDEV won’t start
      • 11.7:PHPStorm
        • 11.7.1:Setting up PHPStorm and Drupal
        • 11.7.2:PHPStorm and Xdebug
          • 11.7.2.1:add a breakpoint in code
        • 11.7.3:Collecting PhpStorm debugging logs
      • 11.8:Troubleshooting Xdebug with DDEV
      • 11.9:What is listening on port 9000?
      • 11.10:Setup settings.local.php and disable Cache
      • 11.11:Development.services.yml
      • 11.12:Enable twig debugging output in source
      • 11.13:Kint
        • 11.13.1:Setup
        • 11.13.2:Add kint to a custom module
        • 11.13.3:Dump variables in a TWIG template
        • 11.13.4:Kint::dump
        • 11.13.5:Set max levels to avoid running out of memory
      • 11.14:Replacing deprecated functions
      • 11.15:Missing module
      • 11.16:You have requested a non-existent service
      • 11.17:Resources
    • 12:Email
      • 12.1:Send email
      • 12.2:Using tokens in hook_mail
      • 12.3:Reference
    • 13:Entities
      • 13.1:Overview
      • 13.2:Config entity types
      • 13.3:Content entity types
      • 13.4:Query an entity by title and type
      • 13.5:Create an entity
      • 13.6:Save an entity
      • 13.7:Create article node entity with attached image
      • 13.8:Update a node entity and add some terms
      • 13.9:Get the entity type and content type (or bundle type)
      • 13.10:Identify entities
      • 13.11:Create a file entity
      • 13.12:Entity Validation
      • 13.13:Resources
    • 14:Forms, Form API and AJAX
      • 14.1:Overview
      • 14.2:Find a form id in the page source
      • 14.3:Add buttons to your custom forms
      • 14.4:Modify a button on a form with hook_form_alter
      • 14.5:Hide a field with hook_form_alter
      • 14.6:Hide revision info and moderation state
      • 14.7:Multiple fields on the same controller/page
      • 14.8:Conditional fields and field states API (#states)
        • 14.8.1:Conditional fields in a form
        • 14.8.2:Conditional fields in node add or edit form
      • 14.9:Get the key and value from a select drop-down
      • 14.10:Autocomplete
        • 14.10.1:Add an autocomplete taxonomy field
        • 14.10.2:Add a views-driven entity autocomplete field
        • 14.10.3:Disable autocomplete for user login and password fields
      • 14.11:Validating input
        • 14.11.1:Validate string length
        • 14.11.2:Validate an email
        • 14.11.3:Validate date
        • 14.11.4:Validate a node add or edit
      • 14.12:Displaying Forms
        • 14.12.1:Embedding a form:
        • 14.12.2:Show a form in a block
        • 14.12.3:Provide a block template for a form in a block
      • 14.13:Redirecting
        • 14.13.1:Form submission with redirect
        • 14.13.2:Ajax redirect
        • 14.13.3:AJAX redirect from a select element (dropdown)
      • 14.14:Add Javascript to a form
      • 14.15:AJAX Forms
        • 14.15.1:Popup an AJAX modal dialog
        • 14.15.2:AJAX modal dialog with redirect example
          • 14.15.2.1:Ajax submit
          • 14.15.2.2:Ajax redirect
        • 14.15.3:AJAX redirect from a select element (dropdown)
        • 14.15.4:Update a value in another field(I am I want) using AJAX
        • 14.15.5:I Am I Want revisited
          • 14.15.5.1:Custom responses
        • 14.15.6:How do you find all the possible AJAX commands to use with addCommand()?
        • 14.15.7:Another AJAX Submit example
      • 14.16:Config Forms
        • 14.16.1:Generate a config form with drush
        • 14.16.2:Config forms overview
      • 14.17:The basics of implementing forms
        • 14.17.1:Location
        • 14.17.2:Base Classes for forms
        • 14.17.3:Create your form class by extending Formbase
        • 14.17.4:The main methods
          • 14.17.4.1:getFormId()
          • 14.17.4.2:buildForm()
          • 14.17.4.3:submitForm()
        • 14.17.5:Form validation example #1
        • 14.17.6:Form Validation example #2
        • 14.17.7:Field attributes
        • 14.17.8:Form Elements
        • 14.17.9:Retrieving field values
      • 14.18:Resources
    • 15:General
      • 15.1:Get the current user
      • 15.2:Get the logged in user name and email
      • 15.3:Check if you are on the Front page
      • 15.4:Check if site is in system maintenance mode
      • 15.5:Get Node URL alias or Taxonomy Alias by Node id or Term ID
      • 15.6:Taxonomy alias
      • 15.7:Get current Path
      • 15.8:Get current nid, node type and title
      • 15.9:How to check whether a module is installed or not
      • 15.10:Get current Route name
      • 15.11:Get the current page title
      • 15.12:Get the current user
      • 15.13:Check if you are on the Front page
      • 15.14:Check if site in system maintenance mode
      • 15.15:Retrieve query and get or post parameters ($_POST and $_GET)
      • 15.16:Retrieve URL argument parameters
      • 15.17:Get Current Language in a constructor
      • 15.18:Add a variable to any page on the site
      • 15.19:Add a variable to be rendered in a node.
      • 15.20:Add a bunch of variables to be rendered in a node
      • 15.21:Grabbing entity reference fields in hook_preprocess_node for injection into the twig template
      • 15.22:Render a list created in the template_preprocess_node()
      • 15.23:Indexing paragraphs so you can theme the first one
      • 15.24:Add meta tags using template_preprocess_html
      • 15.25:How to strip % characters from a string
      • 15.26:Remote media entities
      • 15.27:Deprecated functions like drupal_set_message
    • 16:Hooks
      • 16.1:Overview
      • 16.2:Modify the login form
      • 16.3:Modify the node edit form
      • 16.4:Modify fields in a node
      • 16.5:hook_update
      • 16.6:Theme hooks
        • 16.6.1:Hook_preprocess
        • 16.6.2:hook_preprocess_node example 1
        • 16.6.3:hook_preprocess_node example 2
      • 16.7:Organizing your hooks code the OOP way
      • 16.8:Reference
        • 16.8.1:Entity hooks
          • 16.8.1.1:Create operations
          • 16.8.1.2:Read/Load operations
          • 16.8.1.3:Save operations
          • 16.8.1.4:Editing operations
          • 16.8.1.5:Delete operations
          • 16.8.1.6:View/render operations
          • 16.8.1.7:Other entity hooks
        • 16.8.2:Theme hooks
          • 16.8.2.1:Overriding Theme Hooks
          • 16.8.2.2:Preprocessing for Template Files
          • 16.8.2.3:Theme hook suggestions
          • 16.8.2.4:Altering theme hook suggestions
        • 16.8.3:Reference Links
    • 17:Learning and keeping up with Drupal
      • 17.1:Free videos
      • 17.2:Blogs and articles
      • 17.3:Pay videos
      • 17.4:Drupal Training
      • 17.5:Keep up with Drupal news
      • 17.6:Drupal Podcasts
      • 17.7:Books
    • 18:Links, Aliases and URLs
      • 18.1:Create an external url
      • 18.2:Create an internal url
      • 18.3:The Drupal Core Url Class
      • 18.4:The Drupal Core Link Class
        • 18.4.1:Create a link to a node
        • 18.4.2:Create a link to a path with parameters
      • 18.5:Another way to create a link to a node:
      • 18.6:Create a link from an internal URL
      • 18.7:Check if a link field is empty
      • 18.8:Retrieve a link field from a node or a paragraph
      • 18.9:Retrieve a URL field
        • 18.9.1:External links
        • 18.9.2:Internal links
      • 18.10:Get the NID from a URL Alias
      • 18.11:Get the Taxonomy Term ID from a URL alias
      • 18.12:Get URL alias for a taxonomy term
      • 18.13:Get the User ID from a URL alias
      • 18.14:Get the URL alias for a node
      • 18.15:Create a Node Alias
      • 18.16:Get the current Path
      • 18.17:Get current nid, node type and title
      • 18.18:How to get current Route name
      • 18.19:Get current Document root path
      • 18.20:Retrieve URL argument parameters
      • 18.21:Retrieve query and GET or POST parameters ($_POST and $_GET)
      • 18.22:Modify URL Aliases programmatically with hook_pathauto_alias_alter
      • 18.23:Drupal l() is deprecated
      • 18.24:Reference links
    • 19:Logging
      • 19.1:Quick log to watchdog
      • 19.2:Log an email notification was sent to the the email address for the site.
      • 19.3:Logging from a service using dependency injection
      • 19.4:Another example using the logging via dependency injection
      • 19.5:Logging exceptions from a try catch block
      • 19.6:Display a message in the notification area
      • 19.7:Display a variable while debugging
      • 19.8:Reference
    • 20:Menus
      • 20.1:Dynamically change menu items with hook_preprocess_menu
      • 20.2:Permanently update menu links in a hook_update using entityQuery
      • 20.3:Add menu items with hook_update
      • 20.4:Permanently modify or delete menu items with hook_update
      • 20.5:Peer up a menu to its parents to see if it is a child of a content type
      • 20.6:Find all the children of a menu
      • 20.7:Build a menu and all its children
      • 20.8:Create custom Twig extension for rendering a menu
      • 20.9:Active Trail
      • 20.10:Get a node’s menu item and more
      • 20.11:Create menu items in your custom module
      • 20.12:Resources
    • 21:Migration
      • 21.1:Import content from another Migration into Paragraphs
      • 21.2:Resources
    • 22:Modal Dialogs
      • 22.1:Overview
      • 22.2:Dialog title
      • 22.3:Links to slide-in dialogs
      • 22.4:Modal dialog example
        • 22.4.1:Passing entities as parameters
      • 22.5:Modal form example
      • 22.6:Slide-in dialog/Off-canvas dialog
      • 22.7:Slide-in Dialog Example
      • 22.8:Block with a link to popup a custom modal dialog
      • 22.9:No-code modal dialogs
      • 22.10:Resources
    • 23:Nodes and Fields
      • 23.1:Load a node and get a formatted text field
      • 23.2:Load a numeric field value
      • 23.3:Set field values
      • 23.4:Get current page title
      • 23.5:Test if variable is a node
      • 23.6:Get the current nid, node type and title
      • 23.7:Retrieve current node id (nid)
      • 23.8:Retrieve node info from current path
      • 23.9:Load the current node and get it’s node id (nid), field, type
      • 23.10:Load a node by nid and get its title, type and a field
      • 23.11:Load the current node and get the nid, field, type
      • 23.12:Load the user id (uid) for a node
      • 23.13:Test if a field is empty
      • 23.14:Load a node and update a field
      • 23.15:Load values from a date range field
      • 23.16:Load multivalue field
        • 23.16.1:Iterate through results
        • 23.16.2:Read a specific instance
      • 23.17:Update a multivalue field
        • 23.17.1:Function to read and write multivalue fields
        • 23.17.2:Save multivalue field, entity reference field
        • 23.17.3:Update a multivalue entity reference fields
        • 23.17.4:Generic Multivalue field writer
      • 23.18:Does this field exist in my entity?
      • 23.19:Get URL for an image or file in a media reference field
      • 23.20:Retrieve info about a file field
      • 23.21:Retrieve a link field
      • 23.22:Does this field exist in my entity?
      • 23.23:Create a node and write it to the database
      • 23.24:Create a node with an image
      • 23.25:Write a node with an attached file
      • 23.26:Write a date or datetime to a node
      • 23.27:Or just a date (no time)
      • 23.28:Set field values
      • 23.29:Set an entity reference field
      • 23.30:Set multivalue fields (regular and entity reference)
      • 23.31:Clear a text field
      • 23.32:Set or clear a body field
      • 23.33:Load a node and retrieve an entity reference node and nid (target_id)
        • 23.33.1:Load a multivalue reference field.
      • 23.34:Entity reference nodes and their fields
      • 23.35:Load the taxonomy terms from a term reference field
      • 23.36:Load a node and find the terms referenced in a paragraph in a term reference field
      • 23.37:Retrieve a URL field
        • 23.37.1:External links
        • 23.37.2:Internal links
      • 23.38:Load a node and retrieve a paragraph field
      • 23.39:How to get Node URL alias or Taxonomy Alias by Node id or Term ID
      • 23.40:How to set a URL Alias
      • 23.41:Get a node’s menu item and more
      • 23.42:Find a node using it’s uuid
      • 23.43:Retrieve Node ID(NID) or Taxonomy term ID from a Drupal alias or path
      • 23.44:Retrieve all nodes with a matching taxonomy term
      • 23.45:How to uncache a particular page or node
      • 23.46:Get boolean Field
      • 23.47:Date Field
      • 23.48:Date Fields
      • 23.49:Date Range Field doesn’t display correct timezone
      • 23.50:Date Range
      • 23.51:Date Range fields: Load start and end values
      • 23.52:Date Fields: Load or save them
      • 23.53:Comparing DrupalDateTime values
      • 23.54:Date with embedded timezone
      • 23.55:Has something expired?
      • 23.56:Load or save Drupal Date fields
      • 23.57:Retrieve node creation date and format it
      • 23.58:Retrieve node creation or changed date and format it
      • 23.59:Date Field and Date with no time (remove time)
      • 23.60:Smart date (smart_date) load and format
      • 23.61:Smart date (smart_date) all-day
      • 23.62:Smart date (smart_date) range of values
      • 23.63:hook_node_presave or hook_entity_type_presave
      • 23.64:Disable caching for a content type
      • 23.65:Writing some JSON data into a long text field
      • 23.66:Create a node with an image
      • 23.67:Paragraphs
      • 23.68:Load a node and find the terms referenced in a paragraph in a term reference field
      • 23.69:Custom Field Formatter
      • 23.70:Puzzles
        • 23.70.1:What can I do with a call to first() on an entity reference field?
      • 23.71:Great Cheat sheets
    • 24:Getting off the Island (formerly Reaching out of Drupal)
      • 24.1:Overview
      • 24.2:Guzzle example
      • 24.3:Guzzle POST example
      • 24.4:Magic methods to send synchronous requests
      • 24.5:HTTP basic authentication
      • 24.6:Exception handling
      • 24.7:Guzzle Exceptions
      • 24.8:HTTP response status codes
      • 24.9:Reading from an API
      • 24.10:Download a file using guzzle
      • 24.11:Download a file using curl in PHP
      • 24.12:Resources
    • 25:PHP
      • 25.1:Foreach with ampersand (&)
      • 25.2:Deep merge arrays with numeric keys
      • 25.3:Reference
    • 26:Queries
      • 26.1:entityQuery
        • 26.1.1:Find matching nodes - example 1
        • 26.1.2:Find matching nodes - example 2
        • 26.1.3:Find matching article nodes–example 3
        • 26.1.4:Find nodes that match a taxonomy term
        • 26.1.5:Find 5 nodes that have a matching taxonomy term
        • 26.1.6:Find matching nodes and delete them
        • 26.1.7:Slice up entityQuery results into batches of 100 nodes
        • 26.1.8:Query the creation date (among other things) using entityQuery
        • 26.1.9:entityQuery frequently used conditions
        • 26.1.10:Update menu items programatically
        • 26.1.11:Query multi-value fields
        • 26.1.12:Query entity reference fields if they have a value or no value
        • 26.1.13:Query entity reference fields
      • 26.2:Static and dynamic Queries
        • 26.2.1:Static Queries
        • 26.2.2:Get a connection object
        • 26.2.3:SQL select example
        • 26.2.4:Find the biggest value in a field
        • 26.2.5:SQL update query - example 1
        • 26.2.6:SQL update query - example 2
        • 26.2.7:SQL update query - example 3
        • 26.2.8:SQL insert
        • 26.2.9:SQL Insert Query
        • 26.2.10:SQL Delete query
        • 26.2.11:Paragraph query
        • 26.2.12:Create a custom table for your module
      • 26.3:Reference
    • 27:Redirects
      • 27.1:Redirect to an internal url
      • 27.2:Redirect in a form
      • 27.3:Redirect off-site (to a third-party URL)
      • 27.4:Redirect to an existing route with an anchor (or fragment)
      • 27.5:Redirect to a complex route
      • 27.6:Redirect in a controller
      • 27.7:Redirect user after login
      • 27.8:Redirect to the 403 or 404 page
      • 27.9:Redirect to a new page after node operation
      • 27.10:Redirect dynamically to wherever you came from
    • 28:Render Arrays
      • 28.1:Overview
      • 28.2:Overview of the Theme system and Render API.
      • 28.3:Caching
      • 28.4:Properties
      • 28.5:Image
      • 28.6:Simple Text
      • 28.7:Text with variable substitution (Placeholders)
      • 28.8:Wrap an element with a div with a class
      • 28.9:Prefix and suffix
      • 28.10:Date
      • 28.11:Image
      • 28.12:Several Url’s.
      • 28.13:Two paragraphs
      • 28.14:A button that opens a modal dialog
      • 28.15:A link
      • 28.16:A link with a class
      • 28.17:A link and its TWIG template
      • 28.18:A link with parameters and a template file
      • 28.19:Simple unordered list
      • 28.20:Unordered list of links for a menu
      • 28.21:Nested Unordered List
      • 28.22:Select (dropdown)
      • 28.23:Select (dropdown) Ajaxified
      • 28.24:Limit allowed tags in markup
      • 28.25:Disable an element
      • 28.26:Resources
    • 29:Routes and Controllers
      • 29.1:Overview
        • 29.1.1:Route
        • 29.1.2:Controller
        • 29.1.3:Connecting to a twig template
      • 29.2:Simple page without arguments
      • 29.3:Page with arguments
      • 29.4:Simple form
      • 29.5:Admin form (or settings form)
      • 29.6:Routing permissions
        • 29.6.1:A specific permission
        • 29.6.2:Multiple permissions
      • 29.7:Set the page title dynamically
      • 29.8:Disable caching on a route
      • 29.9:Generate route and controller with Drush
      • 29.10:Finding routes with Drush
        • 29.10.1:All routes
        • 29.10.2:Specific path
        • 29.10.3:Specific route name
      • 29.11:Getting some help from Chat GPT
      • 29.12:Resources
    • 30:Setting up your Mac for Drupal development
      • 30.1:Overview
      • 30.2:Better start with these
        • 30.2.1:Display files that start with .
        • 30.2.2:Set fast keyboard repeat and short delay
        • 30.2.3:Display path bar at bottom of finder window
        • 30.2.4:Set main display monitor
      • 30.3:Show speaker icon in menu bar
      • 30.4:SSH Keys
      • 30.5:Homebrew
      • 30.6:PHP
      • 30.7:Composer
      • 30.8:Browsers
      • 30.9:Dev tools
      • 30.10:DDEV
      • 30.11:Sequel Ace
      • 30.12:Terminal
        • 30.12.1:Iterm2 Terminal Replacement
        • 30.12.2:Oh My ZSH
          • 30.12.2.1:ZSH auto suggestion plugin
          • 30.12.2.2:ZSH syntax highlighting
      • 30.13:Command line tools
        • 30.13.1:git
          • 30.13.1.1:.gitconfig
          • 30.13.1.2:.gitignore_global
        • 30.13.2:NVM (Node Version Manager)
        • 30.13.3:bat
        • 30.13.4:cloc
        • 30.13.5:acli
        • 30.13.6:jq
        • 30.13.7:wget
      • 30.14:Drush
        • 30.14.1:Global Drush
      • 30.15:Drupal Check
      • 30.16:PhpStorm
        • 30.16.1:Plugins
        • 30.16.2:Code Sniffing
      • 30.17:Super useful utilities
        • 30.17.1:Stats
        • 30.17.2:ngrok
        • 30.17.3:rectangle
      • 30.18:Resources
    • 31:Services and Dependency Injection
      • 31.1:Overview
      • 31.2:Static
      • 31.3:Static Shorthand methods
      • 31.4:Services in action
      • 31.5:ControllerBase shortcuts
      • 31.6:Injected/Dependency Injection
        • 31.6.1:Controller details
        • 31.6.2:Controller Example 1
        • 31.6.3:Controller Example 2
      • 31.7:Finding services
      • 31.8:Creating a custom service
        • 31.8.1:Arguments
        • 31.8.2:Passing the config factory to our service
        • 31.8.3:Taxonomy Tree Custom Service
      • 31.9:Using your custom service
      • 31.10:Dependency Injection
        • 31.10.1:Overview
        • 31.10.2:Service Container
        • 31.10.3:Controller Example 1
        • 31.10.4:Controller Example 2
        • 31.10.5:Blocks and other plugins
      • 31.11:Procedural to Class-based dependency injection
      • 31.12:Drush services commands
        • 31.12.1:List all services
        • 31.12.2:Generate custom service
      • 31.13:Resources
    • 32:State API, TempStore and UserData
      • 32.1:Overview
      • 32.2:State API
        • 32.2.1:Using Drush to read the State API
        • 32.2.2:Example accessing State API
        • 32.2.3:Long strings broken into paragraphs
      • 32.3:UserData
      • 32.4:TempStore
        • 32.4.1:PrivateTempStore
        • 32.4.2:SharedTempStore
          • 32.4.2.1:Injecting tempstore.shared
      • 32.5:Reference
    • 33:Taxonomy
      • 33.1:Lookup term by name
      • 33.2:Lookup term name using its tid
      • 33.3:Lookup term using its uuid
      • 33.4:Load terms from a term reference field
      • 33.5:Find terms referenced in a paragraph in a term reference field
      • 33.6:Get URL alias from a term ID
      • 33.7:Load all terms for a vocabulary
      • 33.8:Load all terms for a vocabulary and put them in a select (dropdown)
      • 33.9:Create taxonomy term programatically
      • 33.10:Find all nodes with a matching term
      • 33.11:Find nodes with a matching term using entityQuery
    • 34:TWIG
      • 34.1:Overview
        • 34.1.1:Theme System Overview
        • 34.1.2:Twig Templating Engine
      • 34.2:Displaying Data
        • 34.2.1:Fields or Logic
        • 34.2.2:Which template, which variables?
        • 34.2.3:Display fields or variables
        • 34.2.4:Node Title with and without a link
        • 34.2.5:Fields
        • 34.2.6:Paragraph field
        • 34.2.7:Loop thru paragraph reference fields
        • 34.2.8:Body
        • 34.2.9:Multi-value fields
        • 34.2.10:Fields with HTML
        • 34.2.11:The date/time a node is published, updated or created
        • 34.2.12:Format a date field
        • 34.2.13:Smart date field formatting
        • 34.2.14:Entity Reference field
        • 34.2.15:Entity reference destination content
        • 34.2.16:Taxonomy term
        • 34.2.17:Render a block
        • 34.2.18:Render a list created in the template_preprocess_node()
        • 34.2.19:Links
        • 34.2.20:Links to other pages on site
        • 34.2.21:Link to a user using user id
        • 34.2.22:External link in a field via an entity reference
        • 34.2.23:Render an internal link programatically
        • 34.2.24:Render an image with an image style
        • 34.2.25:Hide if there is no content in a field or image
        • 34.2.26:Hide if there is no image present
        • 34.2.27:Attributes
        • 34.2.28:Output the content but leave off the field_image
        • 34.2.29:Add a class
        • 34.2.30:Add a class conditionally
        • 34.2.31:Links to other pages on site
        • 34.2.32:Loop.index in a paragraph twig template
        • 34.2.33:Loop thru an array of items with a separator
      • 34.3:Add Javascript into a twig template
      • 34.4:Control/Logic
        • 34.4.1:Concatenate values into a string with join
        • 34.4.2:Include partial templates
        • 34.4.3:Loop through entity reference items
        • 34.4.4:IF OR
        • 34.4.5:Test if a formatted text field is empty
        • 34.4.6:Test empty variable
        • 34.4.7:Conditionals (empty, defined, even)
        • 34.4.8:Test if a paragraph is empty using striptags
        • 34.4.9:Comparing strings
        • 34.4.10:Include other templates as partials
        • 34.4.11:Check if an attribute has a class
        • 34.4.12:Remove an attribute
        • 34.4.13:Convert attributes to array
      • 34.5:Views
        • 34.5.1:Render a view with contextual filter
        • 34.5.2:Count how many rows returned from a view
        • 34.5.3:If view results empty, show a different view
        • 34.5.4:Selectively pass 1 termid or 2 to a view as the contextual filter
        • 34.5.5:Views templates
        • 34.5.6:Inject variables
          • 34.5.6.1:Same field used twice
        • 34.5.7:Concatenate values into a string with join
        • 34.5.8:Loop through entity reference items
      • 34.6:Twig filters and functions
      • 34.7:Twig Tweak
        • 34.7.1:Documentation
        • 34.7.2:Display a block with twig_tweak
        • 34.7.3:Display filter form block
        • 34.7.4:Embed view in twig template
        • 34.7.5:Some tricky quotes magic
      • 34.8:Troubleshooting
        • 34.8.1:Enable Twig debugging and disable caches
        • 34.8.2:Debugging - Dump a variable
        • 34.8.3:Dump taxonomy reference field
        • 34.8.4:Using kint or dump to display variable in a template
        • 34.8.5:502 bad gateway error
        • 34.8.6:Views error
        • 34.8.7:Striptags (when twig debug info causes if to fail)
      • 34.9:Reference

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $14 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub