/**
  * Handles the request for an item
  * @param {GridField} $gridField Grid Field reference
  * @param {SS_HTTPRequest} $request HTTP Request
  * @return {mixed} Returns the result of handleRequest on the item request handler
  */
 public function handleItem($gridField, $request)
 {
     $controller = $gridField->getForm()->Controller();
     if (is_numeric($request->param('ID'))) {
         $record = $gridField->getList()->byId($request->param("ID"));
     } else {
         if ($request->getVar('ItemType')) {
             if ($request->getVar('ItemType') == $gridField->getModelClass() || is_subclass_of($request->getVar('ItemType'), $gridField->getModelClass())) {
                 $record = Object::create($request->getVar('ItemType'));
             } else {
                 user_error('Class ' . $request->getVar('ItemType') . ' is not a sub class of ' . $gridField->getModelClass(), E_USER_ERROR);
             }
         } else {
             user_error('No item type selected', E_USER_ERROR);
         }
     }
     $class = $this->getItemRequestClass();
     $handler = Object::create($class, $gridField, $this, $record, $controller, $this->name);
     $handler->setTemplate($this->template);
     return $handler->handleRequest($request, DataModel::inst());
 }
 /**
  * Handles a field request
  * @param {SS_HTTPRequest} $request
  * @return {FormField}
  */
 public function handleField($request)
 {
     $className = $request->param('Type');
     $fieldName = rawurldecode($request->param('FieldName'));
     $realFieldName = preg_replace('/^Widget\\[(.*?)\\]\\[(.*?)\\]\\[(.*?)\\]$/', '$3', $fieldName);
     $baseURL = preg_replace('/\\?(.*?)$/', '', $this->Link('field'));
     //Get the base link stripping parameters
     $baseURL = Controller::join_links($baseURL, $className, 'field', $request->param('FieldName'), '/');
     //Parse field name for the id
     $objId = preg_replace('/Widget\\[(.*?)\\]\\[(.*?)\\]\\[(.*?)\\]/', '$2', $fieldName);
     if (class_exists($className) && is_subclass_of($className, 'Widget')) {
         if (is_numeric($objId)) {
             $obj = $this->UsedWidgets()->byID(intval($objId));
             if (empty($obj) || $obj === false || $obj->ID == 0) {
                 return;
             }
         } else {
             $obj = singleton($className);
         }
     } else {
         return $this->httpError(404, 'Widget not found');
     }
     $field = $obj->getCMSFields()->dataFieldByName($realFieldName);
     if ($field) {
         $field->setForm($this->getFormShiv($obj));
         //Replace the request, we need the post variables to appear as if the widgets are in the top field
         $request = $this->getFakeRequest($request, $obj, $baseURL);
         //Shift the real request by the remaining positions, we assume the field will handle the rest
         $this->request->shift(substr_count($this->request->remaining(), '/') + 1);
         $field->setName('Widget[' . $this->getName() . '][' . $objId . '][' . $field->getName() . ']');
         //Fix the gridstate field
         if ($field instanceof GridField) {
             $field->getState(false)->setName($field->getName() . '[GridState]');
         }
         return $field->handleRequest($request, $this->model);
     } else {
         // falling back to fieldByName, e.g. for getting tabs
         $field = $obj->getCMSFields()->fieldByName($realFieldName);
         if ($field) {
             $field->setForm($this->getFormShiv($obj));
             $request = $this->getFakeRequest($request, $obj, $baseURL);
             //Shift the real request by the remaining positions, we assume the field will handle the rest
             $this->request->shift(substr_count($this->request->remaining(), '/') + 1);
             $field->setName('Widget[' . $this->getName() . '][' . $objId . '][' . $field->getName() . ']');
             //Fix the gridstate field
             if ($field instanceof GridField) {
                 $field->getState(false)->setName($field->getName() . '[GridState]');
             }
             return $field->handleRequest($request, $this->model);
         }
     }
     return $this->httpError(404, 'Widget field not found');
 }