Skip to content

API Reference

shabib edited this page Nov 7, 2026 · 1 revision

API Reference

This page documents the public helper functions provided by the Backdrop Theme Setup library.

The library provides reusable PHP functions for importing sample data and applying selected configuration changes through Backdrop CMS APIs.

Project status: Proof of concept. Function signatures and behavior may change during development.

1. Loading the Library

The library is located at:

includes/theme-setup.inc

Theme developers can load it from their theme settings code:

$theme = 'backdrop_theme_setup';

$library = backdrop_get_path('theme', $theme) . '/includes/theme-setup.inc';

require_once $library;

When integrating the library into another theme, replace backdrop_theme_setup with that theme's machine name.

The public library functions use the theme_setup_ prefix independently of the theme machine name.

2. Reading JSON Configuration

theme_setup_read_json()

theme_setup_read_json($theme, $relative_path);

Reads a JSON configuration file from the theme's settings-content directory.

Parameters:

  • $theme: Theme machine name.
  • $relative_path: Relative path to the JSON file.

Example:

$data = theme_setup_read_json(
  'backdrop_theme_setup',
  'taxonomy/terms.json'
);

Returns the decoded configuration array on success, or FALSE when loading or validation fails.

The helper validates the requested path to prevent access outside the permitted configuration directory.

3. Importing Taxonomy Terms

theme_setup_import_terms()

theme_setup_import_terms($theme);

Reads:

settings-content/taxonomy/terms.json

Creates taxonomy terms in existing vocabularies and supports configured URL aliases.

Example:

theme_setup_import_terms('backdrop_theme_setup');

The target vocabularies must already exist.

Use native Backdrop configuration files in the theme's config directory when additional vocabularies are required.

4. Importing Managed Files

theme_setup_import_file()

theme_setup_import_file($theme, $relative_path, $scheme = 'public');

Imports a file from the theme's sample files directory into Backdrop's managed file system.

Parameters:

  • $theme: Theme machine name.
  • $relative_path: Relative file path.
  • $scheme: Destination file scheme, defaulting to public.

Example:

$file = theme_setup_import_file(
  'backdrop_theme_setup',
  'example.jpg'
);

The helper checks file paths and can reuse existing managed files at the expected destination.

5. Preparing Field Values

theme_setup_prepare_field()

theme_setup_prepare_field($theme, $field_name, $value);

Prepares supported field values for content import.

The helper handles several common field types, including:

  • Text fields.
  • Numeric fields.
  • List fields.
  • Taxonomy term references.
  • Image fields.
  • File fields.
  • Supported native field value arrays.

Example:

$value = theme_setup_prepare_field(
  'backdrop_theme_setup',
  'field_image',
  'example.jpg'
);

The exact input format depends on the field type.

Field definitions and field instances must exist before importing content that uses them.

6. Importing Sample Content

theme_setup_import_content()

theme_setup_import_content($theme);

Reads:

settings-content/content/content.json

Creates sample nodes using existing content types and fields.

Example:

theme_setup_import_content('backdrop_theme_setup');

The importer supports configured node properties, field values, URL aliases, and managed files.

Existing URL aliases are checked to reduce duplicate imports.

This is an initial content importer, not a complete content synchronization or migration framework.

7. Importing Menu Links

theme_setup_import_menu_links()

theme_setup_import_menu_links($theme);

Reads:

settings-content/menu/links.json

Creates menu links in existing menus.

Example:

theme_setup_import_menu_links('backdrop_theme_setup');

Supported behavior includes:

  • Creating links.
  • Configuring link weights.
  • Supporting parent-child relationships.
  • Checking for matching existing links.

Required menus and destination paths should be available before running the import.

8. Updating Views Taxonomy Filters

theme_setup_update_views_filters()

theme_setup_update_views_filters($theme);

Reads:

settings-content/views/filters.json

Updates configured taxonomy filters in existing Views.

Example:

theme_setup_update_views_filters('backdrop_theme_setup');

The helper resolves taxonomy term IDs dynamically using term names and vocabularies.

This avoids relying on fixed term IDs across different installations.

The Views, displays, filters, and referenced terms must already exist.

This function does not create new Views.

9. Applying Layout Settings

theme_setup_apply_layout_settings()

theme_setup_apply_layout_settings($theme);

Reads:

settings-content/layouts/settings.json

Applies supported settings to existing Backdrop Layouts.

Example:

theme_setup_apply_layout_settings('backdrop_theme_setup');

Supported settings include Layout templates and page title configuration.

The target Layouts and templates must be available.

10. Configuring Layout Blocks

theme_setup_configure_layout_blocks()

theme_setup_configure_layout_blocks($theme);

Reads:

settings-content/layouts/blocks.json

Performs supported block operations in existing Layouts.

Example:

theme_setup_configure_layout_blocks('backdrop_theme_setup');

Supported operations include:

  • Removing matching blocks.
  • Adding blocks.
  • Moving existing blocks.
  • Updating supported block settings.
  • Configuring positions within Layout regions.

The library resets Layout caches when necessary after saving changes.

11. Flushing Image Styles

theme_setup_flush_image_styles()

theme_setup_flush_image_styles($styles = NULL);

Flushes generated image derivatives for selected image styles.

Parameters:

  • $styles: A style name, an array of names, or NULL.

Examples:

Flush one style:

theme_setup_flush_image_styles('thumbnail');

Flush multiple styles:

theme_setup_flush_image_styles(array(
  'thumbnail',
  'medium',
));

Flush all available styles:

theme_setup_flush_image_styles();

The function uses Backdrop's native image style APIs.

It does not require a global cache flush.

12. Error Handling

Library functions perform validation appropriate to their operations.

However, callers should not assume that every setup action is transactional or automatically reversible.

Recommended practices:

  • Check required modules and configuration before running imports.
  • Review status and error messages.
  • Test with a fresh Backdrop installation.
  • Back up existing websites before modifying content or Layouts.
  • Avoid assuming that repeating an operation will restore deleted data.

13. Extending the API

Additional reusable helpers may be introduced as the project develops.

New functions should:

  1. Use the theme_setup_ prefix.
  2. Accept the theme machine name when accessing theme-specific resources.
  3. Use Backdrop APIs.
  4. Validate external configuration data.
  5. Avoid unnecessary global cache clearing.
  6. Minimize changes to existing site data.
  7. Document dependencies and supported behavior.

Repository: https://github.com/shabib-m/backdrop_theme_setup

Issues: https://github.com/shabib-m/backdrop_theme_setup/issues

Clone this wiki locally