Documentation

CRM_Core_Resources_CollectionAdderTrait

Class CRM_Core_Resources_CollectionTrait

This trait is a building-block for creating classes which maintain a list of resources. It defines a set of helper functions which provide syntactic sugar for calling the add() method. It implements most of the CollectionAdderInterface.

Tags
see
CRM_Core_Resources_CollectionAdderInterface

Table of Contents

Methods

add()  : array<string|int, mixed>
Add an item to the collection.
addMarkup()  : static
Add an HTML blob.
addModule()  : static
Add an ECMAScript module (ESM) to the current page (<SCRIPT TYPE=MODULE>).
addModuleFile()  : static
Add an ECMAScript Module (ESM) from file to the current page (<SCRIPT TYPE=MODULE SRC=...>).
addModuleUrl()  : static
Add an ECMAScript Module (ESM) by URL to the current page (<SCRIPT TYPE=MODULE SRC=...>).
addPermissions()  : static
Export permission data to the client to enable smarter GUIs.
addScript()  : static
Add a JavaScript file to the current page using <SCRIPT SRC>.
addScriptFile()  : static
Add a JavaScript file to the current page using <SCRIPT SRC>.
addScriptUrl()  : static
Add a JavaScript URL to the current page using <SCRIPT SRC>.
addSetting()  : static
Add JavaScript variables to the root of the CRM object.
addSettingsFactory()  : static
Add JavaScript variables to the global CRM object via a callback function.
addString()  : static
Add translated string to the js CRM object.
addStyle()  : static
Add a CSS content to the current page using <STYLE>.
addStyleFile()  : static
Add a CSS file to the current page using <LINK HREF>.
addStyleUrl()  : static
Add a CSS file to the current page using <LINK HREF>.
addVars()  : static
Add JavaScript variables to CRM.vars
findCreateSettingSnippet()  : array<string|int, mixed>
Locate the 'settings' snippet.
mergeSettingOptions()  : array<string|int, mixed>
Given the "$options" for "addSetting()" (etal), normalize the contents and potentially add more.
mergeSettings()  : array<string|int, mixed>
mergeStandardOptions()  : array<string|int, mixed>
Given the "$options" for "addScriptUrl()" (etal), normalize the contents and potentially add more.

Methods

addMarkup()

Add an HTML blob.

public addMarkup(string $markup, array<string|int, mixed> ...$options) : static

Ex: addMarkup('

Hello world!

', ['weight' => 123]);

Parameters
$markup : string

HTML code.

$options : array<string|int, mixed>

Open-ended list of key-value options. See CollectionInterface docs. Positional equivalence: addMarkup(string $code, int $weight, string $region).

Tags
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addMarkup()
Return values
static

addModule()

Add an ECMAScript module (ESM) to the current page (<SCRIPT TYPE=MODULE>).

public addModule(string $code, array<string|int, mixed> ...$options) : static

Ex: addModule('alert("Hello world");', ['weight' => 123]);

Parameters
$code : string

JavaScript source code.

$options : array<string|int, mixed>

Open-ended list of key-value options. See CollectionInterface docs. Positional equivalence: addModule(string $code, int $weight, string $region).

Tags
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addModule()
Return values
static

addModuleFile()

Add an ECMAScript Module (ESM) from file to the current page (<SCRIPT TYPE=MODULE SRC=...>).

public addModuleFile(string $ext, string $file, array<string|int, mixed> ...$options) : static

Ex: addModuleFile('myextension', 'myscript.js', ['weight' => 123]);

Parameters
$ext : string

Extension name; use 'civicrm' for core.

$file : string

File path -- relative to the extension base dir.

$options : array<string|int, mixed>

Open-ended list of key-value options. See CollectionInterface docs. Positional equivalence: addModuleFile(string $code, int $weight, string $region, mixed $translate).

Tags
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addModuleFile()
Return values
static

addModuleUrl()

Add an ECMAScript Module (ESM) by URL to the current page (<SCRIPT TYPE=MODULE SRC=...>).

public addModuleUrl(string $url, array<string|int, mixed> ...$options) : static

Ex: addModuleUrl('http://example.com/foo.js', ['weight' => 123])

Parameters
$url : string
$options : array<string|int, mixed>

Open-ended list of key-value options. See CollectionInterface docs. Positional equivalence: addModuleUrl(string $url, int $weight, string $region).

Tags
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addModuleUrl()
Return values
static

addScript()

Add a JavaScript file to the current page using <SCRIPT SRC>.

public addScript(string $code, array<string|int, mixed> ...$options) : static

Ex: addScript('alert("Hello world");', ['weight' => 123]);

Parameters
$code : string

JavaScript source code.

$options : array<string|int, mixed>

Open-ended list of key-value options. See CollectionInterface docs. Positional equivalence: addScript(string $code, int $weight, string $region).

Tags
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addScript()
Return values
static

addScriptFile()

Add a JavaScript file to the current page using <SCRIPT SRC>.

public addScriptFile(string $ext, string $file, array<string|int, mixed> ...$options) : static

Ex: addScriptFile('myextension', 'myscript.js', ['weight' => 123]);

Parameters
$ext : string

Extension name; use 'civicrm' for core.

$file : string

File path -- relative to the extension base dir.

$options : array<string|int, mixed>

Open-ended list of key-value options. See CollectionInterface docs. Positional equivalence: addScriptFile(string $code, int $weight, string $region, mixed $translate).

Tags
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addScriptFile()
Return values
static

addScriptUrl()

Add a JavaScript URL to the current page using <SCRIPT SRC>.

public addScriptUrl(string $url, array<string|int, mixed> ...$options) : static

Ex: addScriptUrl('http://example.com/foo.js', ['weight' => 123])

Parameters
$url : string
$options : array<string|int, mixed>

Open-ended list of key-value options. See CollectionInterface docs. Positional equivalence: addScriptUrl(string $url, int $weight, string $region).

Tags
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addScriptUrl()
Return values
static

addSetting()

Add JavaScript variables to the root of the CRM object.

public addSetting(array<string|int, mixed> $settings, array<string|int, mixed> ...$options) : static

This function is usually reserved for low-level system use. Extensions and components should generally use addVars instead.

Parameters
$settings : array<string|int, mixed>

Data to export.

$options : array<string|int, mixed>

Not used. Positional equivalence: addSetting(array $settings, string $region).

Tags
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addSetting()
Return values
static

addString()

Add translated string to the js CRM object.

public addString(string|array<string|int, mixed> $text[, string|null $domain = 'civicrm' ]) : static

It can then be retrived from the client-side ts() function Variable substitutions can happen from client-side

Note: this function rarely needs to be called directly and is mostly for internal use. See CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files

Simple example: // From php: CRM_Core_Resources::singleton()->addString('Hello'); // The string is now available to javascript code i.e. ts('Hello');

Example with client-side substitutions: // From php: CRM_Core_Resources::singleton()->addString('Your %1 has been %2'); // ts() in javascript works the same as in php, for example: ts('Your %1 has been %2', {1: objectName, 2: actionTaken});

NOTE: This function does not work with server-side substitutions (as this might result in collisions and unwanted variable injections) Instead, use code like: CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs))))); And from javascript access it at CRM.myNamespace.myString

Parameters
$text : string|array<string|int, mixed>
$domain : string|null = 'civicrm'
Tags
see
CRM_Core_Resources_CollectionAdderInterface::addString()
Return values
static

addStyle()

Add a CSS content to the current page using <STYLE>.

public addStyle(string $code, array<string|int, mixed> ...$options) : static

Ex: addStyle('p { color: red; }', ['weight' => 100]);

Parameters
$code : string

CSS source code.

$options : array<string|int, mixed>

Open-ended list of key-value options. See CollectionInterface docs. Positional equivalence: addStyle(string $code, int $weight, string $region).

Tags
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addStyle()
Return values
static

addStyleFile()

Add a CSS file to the current page using <LINK HREF>.

public addStyleFile(string $ext, string $file, array<string|int, mixed> ...$options) : static

Ex: addStyleFile('myextension', 'mystyles.css', ['weight' => 100]);

Parameters
$ext : string

Extension name; use 'civicrm' for core.

$file : string

File path -- relative to the extension base dir.

$options : array<string|int, mixed>

Open-ended list of key-value options. See CollectionInterface docs. Positional equivalence: addStyle(string $code, int $weight, string $region).

Tags
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addStyleFile()
Return values
static

addStyleUrl()

Add a CSS file to the current page using <LINK HREF>.

public addStyleUrl(string $url, array<string|int, mixed> ...$options) : static

Ex: addStyleUrl('http://example.com/foo.css', ['weight' => 100]);

Parameters
$url : string
$options : array<string|int, mixed>

Open-ended list of key-value options. See CollectionInterface docs. Positional equivalence: addStyleUrl(string $code, int $weight, string $region).

Tags
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addStyleUrl()
Return values
static

addVars()

Add JavaScript variables to CRM.vars

public addVars(string $nameSpace, array<string|int, mixed> $vars, array<string|int, mixed> ...$options) : static

Example: From the server: CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar')); Access var from javascript: CRM.vars.myNamespace.foo // "bar"

Parameters
$nameSpace : string

Usually the name of your extension.

$vars : array<string|int, mixed>

Data to export.

$options : array<string|int, mixed>

Open-ended list of key-value options. See CollectionInterface docs. Positional equivalence: addVars(string $namespace, array $vars, string $region).

Tags
see
https://docs.civicrm.org/dev/en/latest/standards/javascript/
see
CRM_Core_Resources_CollectionInterface
see
CRM_Core_Resources_CollectionAdderInterface::addVars()
Return values
static

mergeSettingOptions()

Given the "$options" for "addSetting()" (etal), normalize the contents and potentially add more.

public static mergeSettingOptions(array<string|int, mixed> $splats[, array<string|int, mixed> $defaults = [] ]) : array<string|int, mixed>
Parameters
$splats : array<string|int, mixed>

A list of options, as represented by the splat mechanism ("...$options"). This may appear in one of two ways:

  • New (String Index): as in addFoo($foo, array $options)
  • Old (Numeric Index): as in addFoo($foo, int $weight = X, string $region = Y, bool $translate = X)
$defaults : array<string|int, mixed> = []

List of values to merge into $options.

Return values
array<string|int, mixed>

mergeSettings()

public static mergeSettings(array<string|int, mixed> $settings, array<string|int, mixed> $additions) : array<string|int, mixed>
Parameters
$settings : array<string|int, mixed>
$additions : array<string|int, mixed>
Return values
array<string|int, mixed>

combination of $settings and $additions

mergeStandardOptions()

Given the "$options" for "addScriptUrl()" (etal), normalize the contents and potentially add more.

public static mergeStandardOptions(array<string|int, mixed> $splats[, array<string|int, mixed> $defaults = [] ]) : array<string|int, mixed>
Parameters
$splats : array<string|int, mixed>

A list of options, as represented by the splat mechanism ("...$options"). This may appear in one of two ways:

  • New (String Index): as in addFoo($foo, array $options)
  • Old (Numeric Index): as in addFoo($foo, int $weight = X, string $region = Y, bool $translate = X)
$defaults : array<string|int, mixed> = []

List of values to merge into $options.

Return values
array<string|int, mixed>

        
On this page

Search results