# Collection methods

****

Collection methods are registered with the `collectionMethods` extension.

They are used for all CMS collections (`Files`, `Pages` and `Users` as well as `Structure`, `Languages`...) whenever:

- there is no built-in method or
- a more specific <a href="https://getkirby.com/docs/reference/plugins/extensions/files-methods">files</a>, <a href="https://getkirby.com/docs/reference/plugins/extensions/pages-methods">pages</a> or <a href="https://getkirby.com/docs/reference/plugins/extensions/users-methods">users</a> method.

## Default collection methods

**For a full <a href="https://getkirby.com/docs/reference/objects/cms/collection">list of default collection methods</a>, please check out the Reference.**

<info>Be aware that you cannot override these default collection methods with any custom collection method.</info>

## Getting started

You can extend the set of defined collection methods in a plugin file.

```php "/site/plugins/collection-methods/index.php"
Kirby::plugin('my/plugin', [
    'collectionMethods' => [
         'toCustomArray' => function () {
            return $this->toArray(function ($item) {
                return $item->id();
            });
        }
    ]
]);
```

This example shows the basic architecture of a collection method. You define the method name with the key for the `collectionMethods` extension array. `$this` in the callback function refers to the `$collection` object.

The example will return an array of collection item IDs.

## Working with method arguments

In some cases it might be helpful to be able to pass arguments to the method.

You can define arguments for a method like this:

```php "/site/plugins/collection-methods/index.php"
Kirby::plugin('my/plugin', [
    'collectionMethods' => [
         'toCustomArray' => function ($field) {
            return $this->toArray(fn ($item) => $item->$field());
        }
    ]
]);
```

And then use it like this:

```php
<?= $collection->toCustomArray('title') ?>
```

This example will return the titles of all collection items.