|
Location: Web development - PHP License: The Creative Commons Attribution-ShareAlike 2.5 License Zend Framework: The Dijit EditorPosted by Jason HouleUsing the Dijit WYSIWYG Editor with the Zend Framework |
Skill: BeginnerPosted: 20/07/2009Views: 1159Rating: 0.00 /5Popularity: 0.00 |
| Sign Up to vote for this article |
In one of my current projects, AircraftConnection.com, I decided to use the dijit.Editor WYSIWYG editor in my user's forms. I ran into a few problems and could not find much documentation on the topic.
I was able to find a few posts related to posting the data to the server via a hidden field but these mostly did so via Ajax, which is fine. It just didn't suit my needs. I adopted and manipulated their methods to work in the way I wanted.
In this post I make the assumption that you already have the above installed as well as your ZF project created with the Javascript libraries included.
Getting Started
The application we are going to build will be very simple. We will have a single controller, IndexController, with two actions, indexAction and editAction. We will use the editAction to edit the content that is displayed in the index action. The editAction is where we will display the form with the dijit.Editor.
So, in reality, the only work we are going to do will be in the IndexController::editAction() method. This will handle displaying the form as well as processing the data posted to the server.
The first thing in my list was that I wanted to display the label, description, and field in a different order. The default is label, field, description, which really doesn't make all that much sense. So, the first thing we need to do is create our own Element class and extend the dijit.Editor class. We need to override the loadDefaultDecorators() method.
Editor.php
Basically all we are doing here is changing the value of "placement" for our description to be prepend instead of append. One item to not is the use of "DijitElement" instead of the normal "ViewHelper" since we extending a Dijit element. Now we can create our form. The form will consist of a single editor and a submit button.
As stated, we are going to create a form and add one of the Editor elements we just created as well as a submit button. We will do this by extending Zend_Form.
EditForm.php
And the form is done. If you haven't already, create your editAction function and the edit view.
At this point the view is very simple. We will add some Javacsript to it in just a bit. edit.phtml
Here we are checking to see if dojo is enabled, which we did enable via our form in the init() function. We also set the path to the dojo.js file (change this to wherever this file exists in your project relative to the public directory) and also select a stylesheet to use for the editor. Be sure to echo out the form prior to checking to see if dojo is enabled or it will fail. Now on to the controller.
IndexController.php
All we are doing here is creating an instance of our form, assigning it to the view, and displaying the page. If you bring up your browser and view http://localhost/index/edit/ you will be able to see the form with all of it's fancy tools.
The next thing we need to do is populate the editor with the data we want to edit. In this example we are going to pull the contents of our index page into the editor and save the edited content back to the file. The first thing we need to do is read the content from the file. Let's update our editAction() function to the following.
IndexController.php
Our view is now aware of the contents of the index.phtml file but it is still not available to our editor yet. Here is where a little Javascript trickery happens. Essentially what we are going to do is populate a Javascript variable with the contents of our file. We will then use some Javascript to populate the editor. Since the dijit editor is not a textarea but is actually an iframe we will need to get the body of the iframe and add the contents via dijit's setValue() function. Let's go back to the edit.phtml file.
edit.phtml
Now you can reload the page in your browser and you should see the contents of your index.phtml file there. If you have Firebug, or a similar tool, you can walk through the DOM and take a look at how dijit creates the editor. Notice that there is a hidden field with the name "editor" which just happens to be the name of the form element we created. Dijit creates this hidden field for us to use to pass the contents of the form back to the server.
Now our data is being passed back to the server. We need to update our editAction() function to handle the post data and update the file.
IndexController.php
After you submit the form you will be redirected to your newly updated index page. There you have it. The dijit.Editor in action and we achieved the three goals listed at the beginning. It took me some experimenting to get it working so I hope this helps someone out there!
You can extend the Zend_Dojo_Form_Element_* or Zend_Form_Element_* classes to alter the order of your decorators, add new ones, or override functionality. Inheritance in action!
Getting the editor to do what you want takes a little Javascript magic but the functionality and usability it adds is well worth it. Some users still seem to be pleasantly surprised by having this functionality but it is starting to become common place and in the near future all of our textareas will be replaced by WYSIWYG editors.
This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License
| Jason Houle
| Location: |
Sign up to post message on the article message board!