27 Questions (and Answers) from My First Drupal 8 Site Build

Where do I download a base theme for Drupal 8?

How do I create a new theme in Drupal 8?

How do I declare a new template file in my Drupal 8 theme?

Copy any of these to your custom theme

How do I find out what template files I can use?

Twig debug mode!

How do I turn on twig debug mode?

sites/default/services.yml

parameters:
  twig.config:
    # Twig debugging:
    #
    # When debugging is enabled:
    # - The markup of each Twig template is surrounded by HTML comments that
    #   contain theming information, such as template file name suggestions.
    # - Note that this debugging markup will cause automated tests that directly
    #  check rendered HTML to fail. When running automated tests, 'twig_debug'
    #   should be set to FALSE.
    # - The dump() function can be used in Twig templates to output information
    #   about template variables.
    # - Twig templates are automatically recompiled whenever the source code
    #  changes (see twig_auto_reload below).
    #
    # For more information about debugging Twig templates, see
    # http://drupal.org/node/1906392.
    #
    # Not recommended in production environments
    # @default false
    debug: true
    # Twig auto-reload:
    #
    # Automatically recompile Twig templates whenever the source code changes.
    # If you don't provide a value for twig_auto_reload, it will be determined
    # based on the value of twig_debug.
    #
    #  Not recommended in production environments
    # @default null
    auto_reload: true
    # Twig cache:
    #
    # By default, Twig templates will be compiled and stored in the filesystem
    # to increase performance. Disabling the Twig cache will recompile the
    # templates from source each time they are used. In most cases the
    # twig_auto_reload setting above should be enabled rather than disabling the
    # Twig cache.
    #
    # Not recommended in production environments
    # @default true
    cache: false
  factory.keyvalue:
    {}
    # Default key/value storage service to use.
    # @default keyvalue.database
    #default: keyvalue.database
    # Collection-specific overrides.
    #state: keyvalue.database
  factory.keyvalue.expirable:
    {}
    # Default key/value expirable storage service to use.
    # @default keyvalue.database.expirable
    #default: keyvalue.database.expirable

Why do I have to clear cache after every change anything in my template file?

sites/default/services.yml

parameters:
  twig.config:
    # Twig debugging:
    #
    # When debugging is enabled:
    # - The markup of each Twig template is surrounded by HTML comments that
    #   contain theming information, such as template file name suggestions.
    # - Note that this debugging markup will cause automated tests that directly
    #  check rendered HTML to fail. When running automated tests, 'twig_debug'
    #   should be set to FALSE.
    # - The dump() function can be used in Twig templates to output information
    #   about template variables.
    # - Twig templates are automatically recompiled whenever the source code
    #  changes (see twig_auto_reload below).
    #
    # For more information about debugging Twig templates, see
    # http://drupal.org/node/1906392.
    #
    # Not recommended in production environments
    # @default false
    debug: true
    # Twig auto-reload:
    #
    # Automatically recompile Twig templates whenever the source code changes.
    # If you don't provide a value for twig_auto_reload, it will be determined
    # based on the value of twig_debug.
    #
    #  Not recommended in production environments
    # @default null
    auto_reload: true
    # Twig cache:
    #
    # By default, Twig templates will be compiled and stored in the filesystem
    # to increase performance. Disabling the Twig cache will recompile the
    # templates from source each time they are used. In most cases the
    # twig_auto_reload setting above should be enabled rather than disabling the
    # Twig cache.
    #
    # Not recommended in production environments
    # @default true
    cache: false
  factory.keyvalue:
    {}
    # Default key/value storage service to use.
    # @default keyvalue.database
    #default: keyvalue.database
    # Collection-specific overrides.
    #state: keyvalue.database
  factory.keyvalue.expirable:
    {}
    # Default key/value expirable storage service to use.
    # @default keyvalue.database.expirable
    #default: keyvalue.database.expirable

I did set twig auto_reload to true

Text

Text

create settings.local.php

settings.php

How do I preprocess my template files?

theme_name.theme

<?php

/**
 * Implements hook_preprocess_node
 */
function matt_preprocess_node(&$vars) {
  if ($vars['node']->getType() === 'article') {
    $unformatted_date = $vars['node']->getCreatedTime();
    $vars['date'] = format_date($unformatted_date, 'custom', 'M d, Y');
  }
}

Why am I unable to save services.yml?

Where is jQuery

No seriously where is jQuery

How do I enable jQuery?

theme_name.theme

<?php

/**
 * Add jquery
 */
function matt_page_alter(&$page) {
  $page['#attached']['library'][] = 'core/jquery';
}

(deprecated for hook_page_attachments)

How do I add my own custom Javascript in Drupal 8?

themes/theme_name/theme_name.libraries.yml
matt-corescripts:
  version: VERSION
  js:
    js/modernizr.custom.04204.js: {}
themes/theme_name/theme_name.theme
<?php

/**
 * Add custom scripts
 */
function matt_page_alter(&$page) {
  $page['#attached']['library'][] = 'matt/matt-corescripts';
}

(deprecated for hook_page_attachments)

How do I add external CSS libraries? (e.g. google fonts)

<?php
/**
 *  Implements hook_page_attachments_alter().
 */
function hook_page_attachments_alter(array &$page)
{
  $page['#attached']['css'][] = array(
    'type' => 'external',
    'data' => '//fonts.googleapis.com/css?family=Open+Sans',
    'every_page' => TRUE,
  );
}
themes/theme_name/theme_name.theme

How do I disable commenting on articles?

Delete the field

How do I change the submitted by information?

node.html.twig


  {% if display_submitted %}
    <footer class="node__meta">
      {{ author_picture }}
      <div{{ author_attributes.addClass('node__submitted') }}>
        {% trans %}Submitted by Matt on {{ date|passthrough }}{% endtrans %}
        {{ metadata }}
      </div>
    </footer>
  {% endif %}

Why can't I disable modules in Drupal 8?

How do I create a custom module in Drupal 8?

(you're asking a lot)

my_module.info.yml

name: Matthew
description: A simple hello world module.
core: 8.x
package: Custom
type: module

my_module.module

<?php
/**
 * @file
 * Code for the matthew.module.
 */

function matthew_page_alter(&$page) {
 drupal_set_message('Hello world');
}

What the hell happened to all the fonts on the node add page?

FIXED!

FIXED!

FIXED!

How do I version my configuration with git?

  drush @yoursite.local config-export
  git add -f sites/default/files/config_wNOLcmycPFZCrXJ9wis9dCdSR4lpYILdBsFxSWuK5Hzhcr-irILQ0u25dfasd9sdfsadWaUDwMg
  git commit -m 'Exporting configuration to code.'
  git push origin master
  drush @yoursite.production config-import

How do I deploy this thing?

Simple

  • Copy DB to prod
  • Copy code to prod
  • Edit setting.php
  • Edit services.yml
  • clear cache

How do I upgrade to php 5.4?

Mac

use homebrew

Ubuntu

use apt-get

Centos

use yum

Why am I getting a white screen of death in production?

Why is `drush cc all` not working?

Why isn't drush working at all with Drupal 8?

How do I upgrade to Drush 7?

OK, I deployed.  Now what?

How do I integrate with Varnish?

Made with Slides.com