Creating a Property Editor
A guide to creating a property editor in Umbraco
This tutorial guides you through creating a property editor, integrating it with Umbraco's Data Types, AngularJS modules and its injector. Finally, it explains how we can test our property editor.
The steps we will go through in part 1 are:
Prerequisites
This tutorial covers how to use AngularJS with Umbraco, so it does not cover AngularJS itself. To read about AngularJS, you can take a look at some of the resources here:
The End Result
We will have a "Suggestions" Data Type in Umbraco. It is registered as a Data Type in the backoffice, and assigned to a Document Type. The Data Type can create and suggest values.
Setting up a plugin
To begin with, let's create a new folder inside /App_Plugins
folder. We will call it Suggestions
.
If you do not have an /App_Plugins
folder, you can create it at the root of your project.
Next, we will create a Package Manifest file to describe what the plugin does. This manifest will tell Umbraco about our new Property Editor and allow us to inject any needed files into the application.
Create the file /App_Plugins/Suggestions/package.manifest
.
For more information about the package.manifest file, see the Package Manifest article.
Inside the package.manifest
file, we will add the following JSON to describe the Property Editor. Have a look at the inline comments in the JSON below for details on each bit:
Setting up a Property Editor with Csharp
You can also create a property editor with C# instead of defining it in a package.manifest
. Create a Suggestion.cs
file at the root of your project to register the editor this way.
As the above C#
code is adding the Property Editor, the package.manifest
file can be simplified like this:
Writing basic HTML and JavaScript
Now, we will add 3 files to the /App_Plugins/Suggestions/
folder:
suggestion.html
suggestion.controller.js
suggestion.css
These will be our main files for the editor, with the .html
file handling the view, .js
file handling the functionality and the .css
file containing the stylesheet.
In the .html
file we'll add:
Optional
Add ng-readonly="readonly"
to the input
tag in order to make the property editor read-only.
In the .js
file, we'll add a basic AngularJS controller declaration
In the .css
file, we'll add:
Now our basic parts of the editor are done, namely:
The package manifest, telling Umbraco what to load
The HTML view for the editor
The controller for wiring up the editor with angular
The stylesheet for defining our data type styles
Registering the Data Type in Umbraco
We will now restart our application. In the Document Type, add a new property called "Suggestion" and add to it the newly created property editor "Suggestions" and save it.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-c7d7e59228a3b5738a1464489ef7601b7f8d350d%252Fsuggestion-property-editor.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=22bdcf0e&sv=1)
Now open the content item of that Document Type and there will be an alert message saying "The controller has landed", which means all is well.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-6d5c95fe366f62b3d116a4f77314608e234bb46a%252FController-landed.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=5ed3c492&sv=1)
We can now edit the assigned property's value with our new property editor.
Implementing AngularJS Dependency Injection
Now, open the suggestion.controller.js
file and edit it so it looks like this:
Visit the Property Editors page for more details about extending this service.
Then update the HTML file with the following, where we add the id to the button:
Now, clear the cache, reload the document, and see the Suggestions Data Type running.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-d05bed6ee46d212cfb6f14a9647b4950a142b26e%252Fsuggestion-editor-backoffice.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=c9f6364b&sv=1)
When we save or publish, the value of the Data Type is automatically synced to the current content object and sent to the server, all through the power of Angular and the ng-model
attribute.
Learn more about extending this service by visiting the Property Editors page.
Last updated