webCOMAND

cPath View Action Overrides

Override the default Open (>) and New (+) actions in a cPath View with the cpath_view::ACTION_OVERRIDES option.

Example

<?php
namespace com_example;

use com_webcomand\models\WebCOMANDApp;
use com_webcomand\components\cpath_view;

class app extends WebCOMANDApp {

    public function launch($options = []) {

        // Display a collection of users in whatever view the
        // system determines is best.
        $this->add_component('\com_webcomand\components\cpath_view', [
            'Container' => 'CenterViewContainer',
            cpath_view::CPATH => '[:User]',
            cpath_view::ACTION_OVERRIDES => [
                cpath_view::ACTION_OPEN => [
                    'request_url' => $this->get_ajax_url() . 'new',
                ]
            ]
        ]);
    }

    // override new behavior
    public function web__new(\com_webcomand\util\ajax $ajax) {
        $repo = $this->framework->repo;
        $action = $ajax->request->post('action');
        $cpath = $ajax->request->post('cpath');
        $content_type_oid = $ajax->request->post('content_type_oid');

        // make sure this is an action we know how to handle
        if($action != 'new') {
            return $ajax->error("Invalid action.  New expected.");
        }

        // create the new object our way
        $object = $repo->new_object('User');
        $object->Username = 'Custom Name';
        $object->store(); // don't approve yet

        // add the object to the cPath
        $parent = $cpath->get_parent_object();
        $field = $cpath->get_parent_field();
        $parent->{$field} = $object;

        $new_cpath = $cpath->append('.'.$field.'/[OID='.$object->OID.']');

        return $ajax->ok([
            'Data' => [
                cpath_view::CPATH => $new_cpath
            ]
        ]);
    }
}

cpath_view::ACTION_OVERRIDES

cpath_view::ACTION_OVERRIDES is included as a key in the options passed to add_component() to override the default behavior of one or more actions with one or more options.

Actions

  • cpath_view::ACTION_OPEN - Override the open action.
  • cpath_view::ACTION_NEW - Override the new action.

Options

Each action above can be assigned an associative array with any of the following options.

  • request_url - The URL of the controller to service AJAX requests for the action, instead of the default controller.  The controller must return an AJAX response with a Data property containing the cPath options for the view resulting from the action.
  • request_object - Associative array that will be converted to JSON and sent with the request data.
  • callback_object - A string that identifies which JavaScript object contains the JavaScript callback method to call once the response is received.  Options are:
    • wc - Call a method of the main webCOMAND object (wc).
    • ui - Call a method of the ui.
    • cpath_ui - Call a method of the cpath_ui component.
  • callback - The method to call on the callback_object.