Backend\Core\Engine\Model::getURLForBlock PHP Method

getURLForBlock() public static method

Get the URL for a give module & action combination
public static getURLForBlock ( string $module, string $action = null, string $language = null, array $data = null ) : string
$module string The module wherefore the URL should be build.
$action string The specific action wherefore the URL should be build.
$language string The language wherein the URL should be retrieved, if not provided we will load the language that was provided in the URL.
$data array An array with keys and values that partially or fully match the data of the block. If it matches multiple versions of that block it will just return the first match.
return string
    public static function getURLForBlock($module, $action = null, $language = null, array $data = null)
    {
        $module = (string) $module;
        $action = $action !== null ? (string) $action : null;
        $language = $language !== null ? (string) $language : BackendLanguage::getWorkingLanguage();
        $pageIdForURL = null;
        $navigation = self::getNavigation($language);
        $dataMatch = false;
        // loop types
        foreach ($navigation as $level) {
            // loop level
            foreach ($level as $pages) {
                // loop pages
                foreach ($pages as $pageId => $properties) {
                    // only process pages with extra_blocks that are visible
                    if (!isset($properties['extra_blocks']) || $properties['hidden']) {
                        continue;
                    }
                    // loop extras
                    foreach ($properties['extra_blocks'] as $extra) {
                        // direct link?
                        if ($extra['module'] == $module && $extra['action'] == $action && $extra['action'] !== null) {
                            // if there is data check if all the requested data matches the extra data
                            if (isset($extra['data']) && $data !== null && array_intersect_assoc($data, (array) $extra['data']) !== $data) {
                                // It is the correct action but has the wrong data
                                continue;
                            }
                            // exact page was found, so return
                            return self::getURL($properties['page_id'], $language);
                        }
                        if ($extra['module'] == $module && $extra['action'] == null) {
                            // if there is data check if all the requested data matches the extra data
                            if (isset($extra['data']) && $data !== null) {
                                if (array_intersect_assoc($data, (array) $extra['data']) !== $data) {
                                    // It is the correct module but has the wrong data
                                    continue;
                                }
                                $pageIdForURL = (int) $pageId;
                                $dataMatch = true;
                            }
                            if ($extra['data'] === null && $data === null) {
                                $pageIdForURL = (int) $pageId;
                                $dataMatch = true;
                            }
                            if (!$dataMatch) {
                                $pageIdForURL = (int) $pageId;
                            }
                        }
                    }
                }
            }
        }
        // still no page id?
        if ($pageIdForURL === null) {
            return self::getURL(404, $language);
        }
        $url = self::getURL($pageIdForURL, $language);
        // set locale with force
        FrontendLanguage::setLocale($language, true);
        // append action
        if ($action !== null) {
            $url .= '/' . urldecode(FrontendLanguage::act(\SpoonFilter::toCamelCase($action)));
        }
        // return the unique URL!
        return $url;
    }

Usage Example

Example #1
0
 /**
  * Add productdata into the comment
  *
  * @param  string $text The comment.
  * @param string $title The title for the product.
  * @param string $URL The URL for the product.
  * @param int $id The id of the comment.
  * @return string
  */
 public static function addProductData($text, $title, $URL, $id)
 {
     // reset URL
     $URL = BackendModel::getURLForBlock('Catalog', 'Detail') . '/' . $URL . '#comment-' . $id;
     // build HTML
     return '<p><em>' . sprintf(BL::msg('CommentOnWithURL'), $URL, $title) . '</em></p>' . "\n" . (string) $text;
 }
All Usage Examples Of Backend\Core\Engine\Model::getURLForBlock