Skip to content

Relative URLs in blocks. #719

@tzyganu

Description

@tzyganu

A lot of the blocks have methods that return relative URLs.
Let's take for example the advanced search block Magento\CatalogSearch\Block\Advanced\Form.
There is this method

public function getSearchPostUrl()
{
    return $this->getUrl('*/*/result');
}

This means that the submit url for the search form (that by the way is submited via GET and the method is called getSearchPostUrl ) is {current_module_key}/{current_controller}/result.
This means I cannot include this block in a custom cms page or block like this:

{{block type="Magento\CatalogSearch\Block\Advanced\Form" template="Magento_CatalogSearch::advanced/form.phtml"}}

Because I'm on a cms page the module key and controller won't match the advanced search ones.
I have to either create my own block that extends Magento\CatalogSearch\Block\Advanced\Form and changes the getSearchPostUrl and use that one, or I have to clone the template and change the form action.

The solutions I'm thinking of are:

  1. Simple and fast: You can change the block method to return $this->getUrl('catalogsearch/advanced/result')
  2. The flexible one, allow dynamic values for urls.
public function getSearchPostUrl()
{
    if ($url = $this->getData('search_post_url')) {
         return $this->getUrl($url);
    }
    return $this->getUrl('*/*/result');
}

This will allow developers to do something like this:

{{block type="Magento\CatalogSearch\Block\Advanced\Form" template="Magento_CatalogSearch::advanced/form.phtml" search_post_url="catalogsearch/advanced/result" }}

This may not be the only case, but it's the first I found.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentimprovement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions