Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 46 additions & 88 deletions 16/umbraco-cms/customizing/property-editors/property-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,104 +8,62 @@ description: Guide on how to implement Property Actions for Property Editors in
This page is a work in progress and may undergo further revisions, updates, or amendments. The information contained herein is subject to change without notice.
{% endhint %}

Property Actions are a built-in feature that provide a generic place for secondary functionality for property editors.
Property Actions are a built-in feature of Umbraco that allow you to add extra functionality to a Property Editor. Think of them as small, secondary actions you can attach to a property without modifying the editor itself.

Property Actions appear as a small button next to the label of the property, which expands to show the available actions. They are defined and implemented in the Property Editor, making it open as to what a Property Action is.
Property Actions appear as a small button next to the property label, which expands to show the available actions.

## Data Structure of Property Actions
## Property Actions in the UI

Property Actions are an array of objects defining each action. An action is defined by the following properties:
<figure style="max-width:60%; margin:auto; text-align:center;">
<img src="../../.gitbook/assets/property-actions-blocklist.png" alt="" style="max-width:60%; height:auto; display:block; margin:auto">
<figcaption><p><strong>Property action in Block List</strong></p></figcaption>
</figure>

```js
{
labelKey: 'clipboard_labelForRemoveAllEntries',
labelTokens: [],
icon: 'trash',
method: removeAllEntries,
isDisabled: true
}
```

We use `labelKey` and `labelTokens` to retrieve a localized string that is displayed as the Actions label. [See localization for more info.](../../extending/language-files/)

`isDisabled` is used to disable an Action, which change the visual appearance and prevents interaction. Use this option when an action wouldn't provide any change. In the example above, the action `remove all entries` would not have any impact if there is no entries.

## Implementation

The implementation of Property Actions varies depending on whether your Property Editor is implemented with a Controller or as a Component.

### Controller Implementation

When your Property Editor is implemented with a Controller, use the following approach for the Property Action:

```js
angular.module("umbraco").controller("My.MarkdownEditorController", function ($scope) {

function myActionExecutionMethod() {
alert('My Custom Property Action Clicked');
// Disable the action so it can not be re-run
// You may have custom logic to enable or disable the action
// Based on number of items selected etc...
myAction.isDisabled = true;
};

var myAction = {
labelKey: 'general_labelForMyAction',
labelTokens: [],
icon: 'action',
method: myActionExecutionMethod,
isDisabled: false
}
## Registering a Property Action

var propertyActions = [
myAction
];
{% hint style="info" %}
Before creating a Property Action, make sure you are familiar with the [Extension Registry in Umbraco](https://docs.umbraco.com/umbraco-cms/customizing/extending-overview/extension-registry/extension-registry).
{% endhint %}

this.$onInit = function () {
if ($scope.umbProperty) {
$scope.umbProperty.setPropertyActions(propertyActions);
Here is how you can register a new Property Action:
```
import { extensionRegistry } from '@umbraco-cms/extension-registry';
import { MyEntityAction } from './my-property-action.api';
const manifest =
{
type: 'propertyAction',
kind: 'default',
alias: 'My.propertyAction',
name: 'My Property Action ',
forPropertyEditorUis: ["my-property-editor"], // Target specific property editors
api: () => import('./my-property-action.api.js'),
weight: 10, // Order if multiple actions exist
meta: {
icon: 'icon-add', // Icon to display in the UI
label: 'My property action', // Label shown to editors
}
};

};

});
extensionRegistry.register(manifest);
```
### Creating the Property Action Class

### Component Implementation
Every Property Action needs a class that defines what happens when the action is executed.
You can extend the `UmbPropertyActionBase` class for this.

Follow this guide if your Property Editor is implemented as a Component. The Component must be configured to retrieve an optional reference to `umbProperty`. The requirement must be optional because property-editors are implemented in scenarios where it's not presented.

See the following example:

```js
angular.module('umbraco').component('myPropertyEditor', {
controller: MyController,
controllerAs: 'vm',
require: {
umbProperty: '?^umbProperty'
}
…
});
```

See the following example for implementation of Property Actions in a Component, notice the difference is that we are parsing actions to `this.umbProperty.setPropertyActions(...)`.

```js
var myAction = {
labelKey: 'general_labelForMyAction',
labelTokens: [],
icon: 'action',
method: myActionExecutionMethod,
isDisabled: false
import { UmbPropertyActionBase } from '@umbraco-cms/backoffice/property-action';
import { UMB_PROPERTY_CONTEXT } from '@umbraco-cms/backoffice/property';

export class MyPropertyAction extends UmbPropertyActionBase {
// The execute method is called when the user triggers the action.
async execute() {
// Retrieve the property’s current state,
const propertyContext = await this.getContext(UMB_PROPERTY_CONTEXT);

// Here it's possible to modify the property or perform other actions. In this case, setting a value.
propertyContext.setValue("Default text here");
}
}

var propertyActions = [
myAction
];

this.$onInit = function () {
if (this.umbProperty) {
this.umbProperty.setPropertyActions(propertyActions);
}
};
```
export { MyPropertyAction as api };
```