Skip to content

Instantly share code, notes, and snippets.

@oksana-c
Last active November 8, 2018 22:07
Show Gist options
  • Select an option

  • Save oksana-c/27f9d14cf99a9ea8ddf03d103078ccb0 to your computer and use it in GitHub Desktop.

Select an option

Save oksana-c/27f9d14cf99a9ea8ddf03d103078ccb0 to your computer and use it in GitHub Desktop.

Revisions

  1. oksana-c revised this gist Nov 7, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion d8-preprocess-process-hooks.md
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ function THEMENAME_preprocess_HOOK(&$variables) {

    ---

    _contents of `theme/process/README.md`_
    _contents of the `theme/process/README.md`_

    # Defining process hooks

  2. oksana-c revised this gist Nov 7, 2018. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions d8-preprocess-process-hooks.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    ---
    contents of `theme/preprocess/README.md`
    _contents of the `theme/preprocess/README.md`_

    ---

    # Defining preprocess hooks
    Rather than placing your preprocess hooks directly in the .theme file you
    @@ -35,9 +33,8 @@ function THEMENAME_preprocess_HOOK(&$variables) {
    ```

    ---
    contents of `theme/process/README.md`

    ---
    _contents of `theme/process/README.md`_

    # Defining process hooks

    @@ -71,6 +68,8 @@ function THEMENAME_process_HOOK(&$variables) {
    }
    ```

    ---

    ## Theme directory structure

    Theme directory structure when preprocess & process hooks are moved out of the .theme file into dedicated directories becomes the following:
  3. oksana-c renamed this gist Nov 7, 2018. 1 changed file with 0 additions and 0 deletions.
  4. oksana-c revised this gist Nov 7, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions d8-preprocess-hooks.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    ---
    contents of `theme/preprocess/README.md`

    ---

    # Defining preprocess hooks
    @@ -35,6 +36,7 @@ function THEMENAME_preprocess_HOOK(&$variables) {

    ---
    contents of `theme/process/README.md`

    ---

    # Defining process hooks
  5. oksana-c revised this gist Nov 7, 2018. 1 changed file with 43 additions and 3 deletions.
    46 changes: 43 additions & 3 deletions d8-preprocess-hooks.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    ---
    contents of `theme/preprocess/README.md`
    ---

    # Defining preprocess hooks
    Rather than placing your preprocess hooks directly in the .theme file you
    can manage them in automatically discovered and lazy-loaded include files. It is
    @@ -29,9 +33,45 @@ function THEMENAME_preprocess_HOOK(&$variables) {
    }
    ```

    ---
    contents of `theme/process/README.md`
    ---

    # Defining process hooks

    Rather than placing your process hooks directly in the .theme file you
    can manage them in automatically discovered and lazy-loaded include files. It is
    even possible to organize them in sub-folders. This feature greatly improves the
    maintainability of large themes that would otherwise contain hundreds of lines
    of unrelated code in your template.php file.

    The include files have to follow a certain naming pattern (HOOK.process.inc)
    for them to be automatically discovered:

    * THEMENAME_process_html() = html.process.inc
    * THEMENAME_process_page() = page.process.inc
    * THEMENAME_process_node() = node.process.inc
    * THEMENAME_process_comment() = comment.process.inc
    * THEMENAME_process_region() = region.process.inc

    As with template files, you should replace underscores from the hook names with
    hyphens:

    * THEMENAME_process_comment_wrapper() = comment-wrapper.process.inc
    * THEMENAME_process_html_tag() = html-tag.process.inc

    Inside of each of these files you define the process hook just as you would
    otherwise do in your template.php file:

    ```
    function THEMENAME_process_HOOK(&$variables) {
    // Your code here.
    }
    ```

    ## Theme directory structure

    Theme directory structure becomes:
    Theme directory structure when preprocess & process hooks are moved out of the .theme file into dedicated directories becomes the following:

    ```
    |-fluffiness.breakpoints.yml
    @@ -50,9 +90,9 @@ Theme directory structure becomes:
    |-images
    | |-buttons.png
    |-logo.svg
    |-**preprocess**
    |-preprocess
    | |-README.md
    |-**process**
    |-process
    | |-README.md
    |-screenshot.png
    |-templates
  6. oksana-c revised this gist Nov 7, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions d8-preprocess-hooks.md
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,7 @@ function THEMENAME_preprocess_HOOK(&$variables) {

    Theme directory structure becomes:

    ```
    |-fluffiness.breakpoints.yml
    |-fluffiness.info.yml
    |-fluffiness.libraries.yml
    @@ -57,3 +58,4 @@ Theme directory structure becomes:
    |-templates
    | |-maintenance-page.html.twig
    | |-node.html.twig
    ```
  7. oksana-c revised this gist Nov 7, 2018. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions d8-preprocess-hooks.md
    Original file line number Diff line number Diff line change
    @@ -28,3 +28,32 @@ function THEMENAME_preprocess_HOOK(&$variables) {
    // Your code here.
    }
    ```

    ## Theme directory structure

    Theme directory structure becomes:

    |-fluffiness.breakpoints.yml
    |-fluffiness.info.yml
    |-fluffiness.libraries.yml
    |-fluffiness.theme
    |-config
    | |-install
    | | |-fluffiness.settings.yml
    | |-schema
    | | |-fluffiness.schema.yml
    |-css
    | |-style.css
    |-js
    | |-fluffiness.js
    |-images
    | |-buttons.png
    |-logo.svg
    |-**preprocess**
    | |-README.md
    |-**process**
    | |-README.md
    |-screenshot.png
    |-templates
    | |-maintenance-page.html.twig
    | |-node.html.twig
  8. oksana-c created this gist Nov 7, 2018.
    30 changes: 30 additions & 0 deletions d8-preprocess-hooks.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    # Defining preprocess hooks
    Rather than placing your preprocess hooks directly in the .theme file you
    can manage them in automatically discovered and lazy-loaded include files. It is
    even possible to organize them in sub-folders. This feature greatly improves the
    maintainability of large themes that would otherwise contain hundreds of lines
    of unrelated code in your template.php file.

    The include files have to follow a certain naming pattern (HOOK.preprocess.inc)
    for them to be automatically discovered:

    * THEMENAME_preprocess_html() = html.preprocess.inc
    * THEMENAME_preprocess_page() = page.preprocess.inc
    * THEMENAME_preprocess_node() = node.preprocess.inc
    * THEMENAME_preprocess_comment() = comment.preprocess.inc
    * THEMENAME_preprocess_region() = region.preprocess.inc

    As with template files, you should replace underscores from the hook names with
    hyphens:

    * THEMENAME_preprocess_comment_wrapper() = comment-wrapper.preprocess.inc
    * THEMENAME_preprocess_html_tag() = html-tag.preprocess.inc

    Inside of each of these files you define the preprocess hook just as you would
    otherwise do in your template.php file:

    ```
    function THEMENAME_preprocess_HOOK(&$variables) {
    // Your code here.
    }
    ```