# Drupal & CiviCRM

# Local Development (DDEV)

## Pre-steps

### New Git repo:

1. Initialize new repo.
2. Take care that repo directory is empty, only .git should be there.
3. Run:  
    ```bash
    ddev config --project-type=drupal11 --docroot=public_html --php-version=8.3
    ddev start
    touch dummy.sql
    ddev import-db --database crm --file dummy.sql
    rm dummy.sql
    ddev composer create drupal/recommended-project:^11 --no-install --no-interaction
    sed -i 's/web\//public_html\//' composer.json
    ddev composer config --no-interaction extra.enable-patching true
    ddev composer config --no-interaction extra.compile-mode all
    ddev composer config --no-plugins --no-interaction allow-plugins.cweagans/composer-patches true
    ddev composer config --no-plugins --no-interaction allow-plugins.civicrm/civicrm-asset-plugin true
    ddev composer config --no-plugins --no-interaction allow-plugins.civicrm/composer-downloads-plugin true
    ddev composer config --no-plugins --no-interaction allow-plugins.civicrm/composer-compile-plugin true
    ddev composer require --no-interaction civicrm/civicrm-{core,packages,drupal-8}
    ddev composer require --no-interaction civicrm/cli-tools
    ddev composer require --no-interaction drush/drush
    mkdir -p public_html/libraries
    ln -s ../../libraries/civicrm/ public_html/libraries/civicrm
    ddev composer install --no-interaction
    ddev drush site:install --account-name=admin --account-pass=admin --locale=nl -y
    ddev drush config:get system.site uuid | awk '{print $2}' | head -1 > site.id # Obtaining site.id for publishing site config to tst/prd
    ddev describe -j |  jq .raw.httpsurl | sed 's/"//g' > site.url # Obtaining site.url for installing CiviCRM
    sed -i "s/# \$settings\['config_sync_directory'\] = '\/directory\/outside\/webroot';/\$settings\['config_sync_directory'\] = '..\/config\/sync';/" public_html/sites/default/settings.php
    ddev exec 'export CIVICRM_L10N_BASEDIR=$PWD/public_html/sites/default/files/civicrm \
    && export CMS_URL=$(cat site.url) \
    && cd public_html \
    && cv core:install --url=$CMS_URL --db="mysql://db:db@db:3306/crm" --lang="nl_NL" -m loadGenerated=1 -f'
    ddev drush config:export
    ddev launch $(ddev drush uli)
    ```
4. When the website is launched, change the password if deemed necessary
5. Add .gitignore with:  
    ```
    .ddev/
    public_html/
    recipes/
    vendor/
    libraries/
    ```

### Existing Repo:

1. Clone the repo
2. Verify composer.json (web root = pubclic\_html)
3. Run:  
    ```bash
    ddev config --project-type=drupal11 --docroot=public_html --php-version=8.3
    ddev start
    touch dummy.sql
    ddev import-db --database crm --file dummy.sql
    rm dummy.sql
    ddev composer config --no-interaction extra.enable-patching true
    ddev composer config --no-interaction extra.compile-mode all
    ddev composer config --no-plugins --no-interaction allow-plugins.cweagans/composer-patches true
    ddev composer config --no-plugins --no-interaction allow-plugins.civicrm/civicrm-asset-plugin true
    ddev composer config --no-plugins --no-interaction allow-plugins.civicrm/composer-downloads-plugin true
    ddev composer config --no-plugins --no-interaction allow-plugins.civicrm/composer-compile-plugin true
    ddev composer require --no-interaction civicrm/civicrm-{core,packages,drupal-8}
    ddev composer require --no-interaction civicrm/cli-tools
    ddev composer require --no-interaction drush/drush
    mkdir -p public_html/libraries
    ln -s ../../libraries/civicrm/ public_html/libraries/civicrm
    ddev composer install --no-interaction
    ddev drush site:install --account-name=admin --account-pass=admin --locale=nl -y
    ddev describe -j |  jq .raw.httpsurl | sed 's/"//g' > site.url # Obtaining site.url for installing CiviCRM
    sed -i "s/# \$settings\['config_sync_directory'\] = '\/directory\/outside\/webroot';/\$settings\['config_sync_directory'\] = '..\/config\/sync';/" public_html/sites/default/settings.php
    ddev exec 'export CMS_URL=$(cat site.url) \
    && cd public_html \
    && cv core:install --url=$CMS_URL --db="mysql://db:db@db:3306/crm" --lang="nl_NL" -m loadGenerated=1 -f'
    set site_id $(cat site.id)
    ddev drush config:set system.site uuid $site_id -y
    sed -i "s/# \$settings\['config_sync_directory'\] = '\/directory\/outside\/webroot';/\$settings\['config_sync_directory'\] = '..\/config\/sync';/" public_html/sites/default/settings.php
    ddev drush config:delete shortcut.set.default -y
    set nl_uuid $(grep uuid config/sync/language.entity.nl.yml | awk '{print $2}')
    ddev drush config:set language.entity.nl uuid $nl_uuid -y
    ddev drush config:import -y
    ddev launch $(ddev drush uli)
    ```

### Updating

```bash
ddev composer update
cd ./public_html
ddev drush updatedb
ddev drush cr

# If CiviCRM had update
curl -Lss -o public_html/sites/default/files/civicrm/l10n/nl_NL/LC_MESSAGES/civicrm.mo https://download.civicrm.org/civicrm-l10n-core/mo/nl_NL/civicrm.mo

ddev exec 'export CIVICRM_SETTINGS=$(find -name civicrm.settings.php) && cv upgrade:db'
rm -rf public_html/sites/default/files/civicrm/templates_c/
ddev exec 'export CIVICRM_SETTINGS=$(find -name civicrm.settings.php) && cv flush'
ddev composer civicrm:publish
```

# Patching

```bash
composer require cweagans/composer-patches
```

```php
    "enable-patching": true,
    "patches": {
      "drupal/core": {
          "Change the LB Add block link text to understand in which region the block will be placed": "https://www.drupal.org/files/issues/2024-07-10/drupal-3460462-9.patch"
      }
        
    }
```

# Installation on mijn.host hosting

Clone git repo first, then install Drupal, providing details manually to the installer.

```bash
composer install
./vendor/bin/drush site:install --locale="nl"
```

Make a note of the provided username and password.

Now export the crm user name and pass

```bash
set +o history
export CRM_DB_USER=
export CRM_DB_PASS=
```

Now install CiviCRM:

```bash
cms_url=$(pwd | awk -F "/" '{print $5}')
ln -s ../../libraries/civicrm public_html/libraries/civicrm
mkdir -p public_html/sites/default/files/civicrm/l10n/nl_NL/LC_MESSAGES
curl -Lss -o public_html/sites/default/files/civicrm/l10n/nl_NL/LC_MESSAGES/civicrm.mo https://download.civicrm.org/civicrm-l10n-core/mo/nl_NL/civicrm.mo
cd public_html
chmod +w sites/default/
set +o history
../vendor/bin/cv core:install --url="https://${cms_url}" --db="mysql://${CRM_DB_USER}:${CRM_DB_PASS}@localhost:3306/${CRM_DB_USER}" --lang="nl_NL"
```

Answer any question that pops up (e.g. db already populated) and run these commands to finish civicrm installation

```bash
set -o history
chmod -w sites/default/
cd ..
./vendor/bin/drush cr
```

Now some manual work:

WIth vi open settings.php file:

```bash
vi public_html/sites/default/settings.php
```

Edit the last row and change the provided path to ../config/sync

```bash
#E.g. change:
$settings['config_sync_directory'] = 'sites/default/files/config_7Mlw0xeTGW_fmBMDS-RmPr29yA3WsBBqcdHxMO_BE1S9DRichbda04ZmUQeV_yJD0tRu4Rk_dA/sync';
#into:
$settings['config_sync_directory'] = '../config/sync';
```

When done, load the site config with the following commands

```bash
site_id=$(cat site.id)
./vendor/bin/drush config:set system.site uuid $site_id -y
./vendor/bin/drush config:delete shortcut.set.default -y
nl_uuid=$(grep uuid ./config/sync/language.entity.nl.yml | awk '{print $2}')
./vendor/bin/drush config:set language.entity.nl uuid $nl_uuid -y
./vendor/bin/drush config:import -y
```

# Updating on mijn.host hosting

```bash
composer install --no-dev
cd ./public_html
export SITE_URL=$(cat ../site.url)
../vendor/bin/drush -l $SITE_URL updatedb
../vendor/bin/drush cr

# If CiviCRM had update
curl -Lss -o sites/default/files/civicrm/l10n/nl_NL/LC_MESSAGES/civicrm.mo https://download.civicrm.org/civicrm-l10n-core/mo/nl_NL/civicrm.mo
../vendor/bin/cv upgrade:db
rm -rf sites/default/files/civicrm/templates_c/
../vendor/bin/cv flush # Ignore the error
composer civicrm:publish
```