/**
  * Custom request handler that will check component handlers before proceeding to the default
  * implementation.
  *
  * @todo copy less code from RequestHandler.
  *
  * @param HTTPRequest $request
  * @param DataModel $model
  *
  * @return array|RequestHandler|HTTPResponse|string|void
  *
  * @throws HTTPResponse_Exception
  */
 public function handleRequest(HTTPRequest $request, DataModel $model)
 {
     if ($this->brokenOnConstruct) {
         user_error(sprintf("parent::__construct() needs to be called on %s::__construct()", __CLASS__), E_USER_WARNING);
     }
     $this->setRequest($request);
     $this->setDataModel($model);
     $fieldData = $this->getRequest()->requestVar($this->getName());
     /** @skipUpgrade */
     if ($fieldData && isset($fieldData['GridState'])) {
         $this->getState(false)->setValue($fieldData['GridState']);
     }
     foreach ($this->getComponents() as $component) {
         if ($component instanceof GridField_URLHandler && ($urlHandlers = $component->getURLHandlers($this))) {
             foreach ($urlHandlers as $rule => $action) {
                 if ($params = $request->match($rule, true)) {
                     // Actions can reference URL parameters.
                     // e.g. '$Action/$ID/$OtherID' → '$Action'
                     if ($action[0] == '$') {
                         $action = $params[substr($action, 1)];
                     }
                     if (!method_exists($component, 'checkAccessAction') || $component->checkAccessAction($action)) {
                         if (!$action) {
                             $action = "index";
                         }
                         if (!is_string($action)) {
                             throw new LogicException(sprintf('Non-string method name: %s', var_export($action, true)));
                         }
                         try {
                             $result = $component->{$action}($this, $request);
                         } catch (HTTPResponse_Exception $responseException) {
                             $result = $responseException->getResponse();
                         }
                         if ($result instanceof HTTPResponse && $result->isError()) {
                             return $result;
                         }
                         if ($this !== $result && !$request->isEmptyPattern($rule) && is_object($result) && $result instanceof RequestHandler) {
                             $returnValue = $result->handleRequest($request, $model);
                             if (is_array($returnValue)) {
                                 throw new LogicException('GridField_URLHandler handlers can\'t return arrays');
                             }
                             return $returnValue;
                         }
                         if ($request->allParsed()) {
                             return $result;
                         }
                         return $this->httpError(404, sprintf('I can\'t handle sub-URLs of a %s object.', get_class($result)));
                     }
                 }
             }
         }
     }
     return parent::handleRequest($request, $model);
 }