CRM_Core_Resources_CollectionInterface
in
Class CRM_Core_Resources_CollectionInterface
A resource collection is a mix of resources (or snippets or assets) that can be added to a page. A fully-formed resource might look like this:
array(
'name' => 'jQuery',
'region' => 'html-header',
'weight' => 100,
'type' => 'scriptUrl',
'scriptUrl' => 'https://example.com/js/jquery.min.js'
)
Typically, a resource is created with just one option, eg
// Add resources in array notation
$c->add(['script' => 'alert("Hello");']);
$c->add(['scriptFile' => ['civicrm', 'js/crm.ckeditor.js']]);
$c->add(['scriptUrl' => 'https://example.com/js/jquery.min.js']);
$c->add(['style' => 'p { font-size: 4em; }']);
$c->add(['styleFile' => ['civicrm', 'css/dashboard.css']]);
$c->add(['styleUrl' => 'https://example.com/css/foobar.css']);
// Add resources with helper methods
$c->addScript('alert("Hello");');
$c->addScriptFile('civicrm', 'js/crm.ckeditor.js');
$c->addScriptUrl('https://example.com/js/jquery.min.js');
$c->addStyle('p { font-size: 4em; }');
$c->addStyleFile('civicrm', 'css/dashboard.css');
$c->addStyleUrl('https://example.com/css/foobar.css');
The other properties are automatically computed (dependent upon context), but they may be set explicitly. These options include:
- type: string (markup, template, callback, script, scriptFile, scriptUrl, jquery, style, styleFile, styleUrl)
- name: string, symbolic identifier for this resource
- aliases: string[], list of alternative names for this resource
- weight: int, default=1. Lower weights come before higher weights. (If two resources have the same weight, then a secondary ordering will be used to ensure reproducibility. However, the secondary ordering is not guaranteed among versions/implementations.)
- disabled: int, default=0
- region: string
- esm: bool, enable ECMAScript Module (ESM) support for "script","scriptFile","scriptUrl"
- translate: bool|string, Autoload translations. (Only applies to 'scriptFile')
- FALSE: Do not load translated strings.
- TRUE: Load translated strings. Use the $ext's default domain.
- string: Load translated strings. Use a specific domain.
For example, the following are equivalent ways to set the 'weight' option:
$c->add([
'script' => 'alert("Hello");',
'weight' => 100,
]);
$c->addScript('alert("Hello");', ['weight' => 100]);
Passing options in array (key-value) notation is clearest. For backward
compatibility, some methods (eg addScript()
) accept options in positional form.
Where applicable, the docblock of each addFoo()
will include a comment about positional form.
Tags
Table of Contents
Methods
- add() : array<string|int, mixed>
- Add an item to the collection. For example, when working with 'page-header' collection:
- clear() : static
- Remove all snippets.
- filter() : static
- Alter the contents of the collection.
- find() : iterable<string|int, mixed>
- Find all snippets which match the given criterion.
- get() : array<string|int, mixed>|null
- Get snippet.
- getAll() : iterable<string|int, mixed>
- Get a list of all snippets in this collection.
- merge() : static
- Assimilate a list of resources into this list.
- update() : static
- Update specific properties of a snippet.
Methods
add()
Add an item to the collection. For example, when working with 'page-header' collection:
public
add(array<string|int, mixed> $snippet) : array<string|int, mixed>
Note: This function does not perform any extra encoding of markup, script code, or etc. If you're passing in user-data, you must clean it yourself.
Parameters
- $snippet : array<string|int, mixed>
-
The resource to add. For a full list of properties, see CRM_Core_Resources_CollectionInterface.
Tags
Return values
array<string|int, mixed> —The full/computed snippet (with defaults applied).
clear()
Remove all snippets.
public
clear() : static
Return values
staticfilter()
Alter the contents of the collection.
public
filter(callable $callback) : static
Parameters
- $callback : callable
-
The callback is invoked once for each member in the collection. The callback may return one of three values:
- TRUE: The item is OK and belongs in the collection.
- FALSE: The item is not OK and should be omitted from the collection.
- Array: The item should be revised (using the returned value).
Return values
staticfind()
Find all snippets which match the given criterion.
public
find(callable $callback) : iterable<string|int, mixed>
Parameters
- $callback : callable
-
The callback is invoked once for each member in the collection. The callback may return one of two values:
- TRUE: The item is OK and belongs in the collection.
- FALSE: The item is not OK and should be omitted from the collection.
Return values
iterable<string|int, mixed> —List of matching snippets.
get()
Get snippet.
public
& get(string $name) : array<string|int, mixed>|null
Parameters
- $name : string
Return values
array<string|int, mixed>|nullgetAll()
Get a list of all snippets in this collection.
public
getAll() : iterable<string|int, mixed>
Return values
iterable<string|int, mixed>merge()
Assimilate a list of resources into this list.
public
merge(iterable<string|int, mixed> $others) : static
Parameters
- $others : iterable<string|int, mixed>
-
List of snippets to add.
Tags
Return values
staticupdate()
Update specific properties of a snippet.
public
update(string $name, array<string|int, mixed> $snippet) : static
Ex: $region->update('default', ['disabled' => TRUE]);
Parameters
- $name : string
-
Symbolic of the resource/snippet to update.
- $snippet : array<string|int, mixed>
-
Resource options. See CollectionInterface docs.