• Development

    Rendering null boolean values with Grid4PHP

    I’m a big fan of Grid4PHP for adding a quick CRUD user interface to a MySQL database.

    I have a lot of boolean columns, like “is_telehealth” that need to start as NULL until we make a determination about the field’s value. Grid4PHP renders null booleans as “0” in the table/edit form. This is misleading, given it’s not actually a 0 value. If you add the ‘isnull’ attribute to the column it lets you save the null value, but it still renders as a “0”.

    Using a custom formatter I was able to render an empty string (nothing) instead of “0” for these null booleans:

  • Development

    Testing Cookie Modification in Laravel 8

    If there’s a better way to pass a cookie from one request to another, in a phpunit feature test in Laravel, please let me know! Here’s one way to handle it:

     

  • Development

    Any Random Saturday Using Faker

    Here’s a quick one, folks. I’m using Faker in Laravel factories to generate realistic data. I have a “week end date” field in one of my models that must always be a Saturday. Here’s how I generate a unique random Saturday:

     

  • Development

    Using Carbon to Generate Random Dates and Times

    There are a number of ways to generate random dates/times in PHP. Here are some examples using the Carbon library:

  • Development

    Remote PHP Debugging with Xdebug + PHPStorm or VSCode on Cloudways

    Here’s a quick breakdown of the steps required to debug a PHP site on a remote Cloudways server.

    Step 1:

    Enable xdebug for the whole Cloudways server:

    Server » Settings & Packages » Advanced » XDEBUG: Enabled

    Step 2:

    For the specific application in Cloudways, add some PHP settings:

    Application » Application Settings » PHP FPM Settings:

    Step 3: Setup SSH config for an ssh tunnel to the server:

    1. Edit ~/.ssh/config

    Step 4 (PHPStorm):

    Configure PHPStorm Preferences

    1. PHP » Debug » Xdebug » Debug port = 9003
    2. PHP » Debug » XdebugCheck all four boxes:
      • Can accept external connections
      • Resolve breakpoint if it’s not available on the current line
      • Force break at first line when no path mapping specified
      • Force break at first line when a script is outside the project
    3. You shouldn’t need to configure servers, PHP cli, deployment locations, or anything similar…

    Try It

    1. Start an SSH tunnel: ssh mhd1_xdebug
    2. Drop a breakpoint in index.php (or whatever will surely execute)
    3. Visit the site, then wait for PHPStorm to prompt you for which file to connect to. Choose accordingly. It should connect and pause. You only have to do this once; it’ll remember the settings and path mapping in PHP » Servers.

    Step 4 (VSCode)

    Configure VSCode Preferences

    1. Install https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug
    2. Click the Run and Debug sidebar icon (⌘⇧D)
    3. SSH into the server to find the exact path to the root of the site (which should match the root of your repo locally, your workspace folder)
    4. Add new configuration (you can disable the log option if things work well right away)

    Try it

    1. Start an SSH tunnel: ssh mhd1_xdebug
    2. Click Listen for Xdebug in the Run and Debug screen
    3. Drop a breakpoint in index.php (or whatever will surely execute)
    4. Visit the site
  • Development

    Basic HTTP Authentication in Drupal Site Using settings.php

    Here’s a quick and painless way of preventing public access to a Drupal site using settings.php (or settings.local.php).

    I’ve been using this for development and staging sites that I want to keep private.

    If you want this to be available to all settings*.php files you should put this near the top of your settings.php file:

    Then, you can leverage it wherever you’d like. For example, on an Acquia site I might add this to the bottom of settings.php:

    For non-Acquia sites I’d call the function at the bottom of settings.local.php.

  • Development

    Setting up OCI8 (PHP Oracle module) on Webfaction

    Webfaction, my favorite web host, allows you to compile PHP modules in your home directory for use on your websites. Here’s the process for configuring OCI8 to talk to Oracle databases:

     

  • Development

    Tideways and Xhgui using DevDesktop and Docker

    THIS POST IS UNFINISHED. Use at your own risk. I needed to share with a colleague, so I’m just getting it out into the world. Your Mileage May Vary!

    I’ve been working on some large Drupal 8 migrations and have been wanting to profile them for some time due to a few migrations taking far more time than I expected. Acquia DevDesktop, which I happen to be using for this site, offers xhprof in the older PHP environments, but I wanted to get something setup in PHP 7.

    For PHP 7 the recommendation is to use Tideways; it’s a modern fork of xhprof. After collecting (tracing/profiling) the data with Tideways you need a way to analyze the data. I used a Docker environment to get Xhgui running. Here’s what a few xhgui screens look like. The best part is that nearly everything is clickable so you can drill down to figure out what’s slow, and why!

  • Development

    Setting up xdebug for PHP 7 in Acquia DevDesktop

    The Acquia DevDesktop help page says:

    The PHP 7 version currently included with Acquia Dev Desktop does not currently include Xdebug. You can download an updated version of Xdebug here .

    Here are the actual steps I used. YMMV.

     

  • Development

    Extracting Image “src” Attributes from an HTML String using PHP

    Here are a few ways to build an array of image sources extracted from an HTML string. I’m sure there are other ways, but these seem to be reliable.