I have a page type 'ProductPage', it has tabs that are navigated to like so:
/ProductPageUrlSegment/?tab=video
/ProductPageUrlSegment/?tab=audio
/ProductPageUrlSegment/?tab=photos
I'd like for redirects to be created when each new product page is created so that if you navigated /ProductPageUrlSegment/video it goes to /ProductPageUrlSegment/?tab=video
and the same for all tabs. I'm not clear if I should be using Routing https://docs.silverstripe.org/en/3.3/developer_guides/controllers/routing/ or redirection https://docs.silverstripe.org/en/3.3/developer_guides/controllers/redirection/
I have a link function for another project which goes to the parent page
public function Link() {
return $this->Parent()->Link() . '#' . $this->URLSegment;
}
Mine would be something like:
public function LinkVideo() {
return $this->Link()->'/?=video' . '#' . $this->URLSegment->'/video';
}
I don't have the knowledge to work this out so any guidance appreciated.
This will achieve the above, by handling the URL as an action and then redirecting to the same page, but with the get variable set...
class ProductPage extends Page {
}
class ProductPage_Controller extends Page_Controller {
private static $allowed_actions = array(
'video',
'audio',
'photos',
);
public function video() {
$this->redirect($this->Link().'?tab=video');
}
public function audio() {
$this->redirect($this->Link().'?tab=audio');
}
public function photos() {
$this->redirect($this->Link().'?tab=photos');
}
}
Related
Like superficial descriped in SilverStripe Docs, I'm trying to set a custom controller for my homepage.
I changed the default homepage link to 'custom-home' and added those two routes.
The second one, with the path in it works and directs me to my controller. The first (empty) one just sends me to an 404-error page.
Couldn't figure out how to fix that. Any suggestions?
routes.yml
Director:
rules:
'': 'MyHome_Controller'
'custom-home': 'MyHome_Controller
_config.php
RootURLController::set_default_homepage_link('custom-home');
MyHome_Controller.php
<?php
class MyHome_Controller extends Page_Controller {
private static $allowed_actions = [];
private static $url_handlers = [];
public function init() {
parent::init();
}
public function Link($action = null) {
return Director::baseURL() . 'custom-home';
}
public function index() {
$data = [
'Title' => 'Hello World',
'ClassName' => __CLASS__,
];
return $this
->customise($data)
->renderWith([__CLASS__, 'Page']);
}
}
I believe the way the empty route (RootURLController) works is that you're telling it the URLSegment of a page in the CMS that should resolve to the root URL. So I think what you need to do is go into the CMS and change the URLSegment of your CustomHomePage to 'custom-home'.
I need interacts with a .tpl file in my adminController class, but when I try to do that, this error appears
Fatal error: Call to undefined method RiddlePageController::getCacheId() in /home/USER/public_html/prestashop/modules/RiddleModule/controllers/admin/RiddlePage.php on line 48
This is my admin controller code:
class RiddlePageController extends AdminController {
public function __construct()
{
$this->html = '';
$this->display = 'view';
$this->meta_title = $this->l('metatitle');
$this->module = "RiddleModule";
parent::__construct();
}
public function initContent()
{
$this->postProcess();
$this->show_toolbar = true;
$this->display = 'view';
$this->meta_title = $this->l('Modulo');
parent::initContent();
}
public function initToolBarTitle()
{
$this->toolbar_title = $this->l('Titulo');
}
public function initToolBar()
{
return true;
}
public function renderView() {
$this->context->smarty->assign(
array(
'img1' => "http://www.free-3dmodels.com/image/Flowers-3D-Model-3662994d.png",
'img2' => "http://www.all3dmodel.com/Images/39.jpg"
)
);
// in return have error "getCacheId"
return $this->display(__FILE__, 'content.tpl', $this->getCacheId());
// return "<b>This works fine!!</b>";
}
my tpl file have only {$img1} and {$img2} for testing.
Maybe I do all wrong, and this is not the best way to make in my own admin page.
Your error is because the AdminController class doesn't have the getCacheId method.
To answer to your question you have to made some little fix.
First (extends ModuleAdminController not AdminController):
class AdminRiddlePageController extends ModuleAdminController
{
}
Then if you want to view your custom tpl, place a view.tpl file in:
prestashop/modules/RiddleModule/views/templates/admin/riddlepage/helpers/view/
or
prestashop/modules/RiddleModule/views/templates/admin/riddle_page/helpers/view/ (I don't remember well if the underscore is necessary)
And your renderView method should be like this:
public function renderView()
{
/* Your code */
/* Use this snippet to assign vars to smarty */
$this->tpl_view_vars = array(
'myvar' => 1,
'secondvar' => true
)
return parent::renderView();
}
AdminController class has not an implementation of display method you use to render TPL.
You can use something like this after set module var:
$this->module->display(_PS_MODULE_DIR_.$this->module->name.DIRECTORY_SEPARATOR.$this->module->name.'.php', 'content.tpl')
Good luck.
As #TheDrot told us, the answer are in using $this->context->smarty->fetch(location), but not in renderList, but in the return statement of renderView is OK and prestashop get the tpl file and load correctly the smarty variables. Ex:
public function renderView(){
$this->context->smarty->assign(
array(
'img1' => "http://www.free-3dmodels.com/image/Flowers-3D-Model-3662994d.png",
'img2' => "http://www.all3dmodel.com/Images/39.jpg"
)
);
return $this->context->smarty->fetch(_PS_MODULE_DIR_ . "RiddleModule/controllers/front/prueba.tpl");
}
The file location isn't important to load the TPL file in this case
I installed and configured SilverStripe on my server. I installed the MultiForm module and followed the instructions in the module documentation.
After following the instructions I still don't see any new page type in my CMS Portal.
I also tried db/build?flush=1 & dev/build?flush=1 but it doesn't make a difference.
I Created the Following files in mysite/code/ directory
SponsorSignupForms.php
class SponsorSignupForms extends MultiForm{
protected static $start_step = 'CompanyDetailsStep';
}
CompanyDetailsStep.php
class CompanyDetailsStep extends MultiFormStep{
public static $next_steps = 'ContactDetailsStep';
function getFields()
{
$fields = singleton('Member')->getFrontendFields();
return $fields;
}
function getValidator()
{
return new Member_Validator('FirstName', 'Surname', 'Email', 'Password');
}
}
ContactDetailsStep.php
class ContactDetailsStep extends MultiFormStep{
public static $is_final_step = true;
function getFields()
{
$fields = singleton('Reference')->getFrontendFields();
return $fields;
}
}
How do I get these custom MultiForms working and appearing as creatable pages?
Of course you don't see any new page type in the list of available pages, you will only see subclasses of SiteTree there, MultiFormStep is "just" a subclass of DataObject.
You can plug your form to every page you want manually, but you also can create a new page type for your form and include the Form in your Controller and Template, see readme of MultiForm:
class MyFormPage extends Page
{
}
class MyFormPageController extends Page_Controller
{
//
private static $allowed_actions = array(
'SponsorSignupForms',
'finished'
);
public function SponsorSignupForms() {
return new SponsorSignupForms($this, 'Form');
}
public function finished() {
return array(
'Title' => 'Thank you for your submission',
'Content' => '<p>You have successfully submitted the form!</p>'
);
}
}
In the template just include the form:
<% if $SponsorSignupForms %>
$SponsorSignupForms
<% end_if %>
and you should see the form now.
This is my controller
class CarsController extends Controller {
public function actionIndex() {
echo 1; exit();
}
}
this is the module file:
<?php
class CarsModule extends CWebModule {
public $defaultController = "cars";
public function init() {
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'cars.models.*',
'cars.components.*',
));
}
public function beforeControllerAction($controller, $action) {
if (parent::beforeControllerAction($controller, $action)) {
// this method is called before any module controller action is performed
// you may place customized code here
return true;
}
else
return false;
}
}
my problem is if I access my project like: localhost/cars it works. If I access localhost/cars/index I am getting this message: Unable to resolve the request . If I create a new function and I access like this: localhost/cars/myfunction, still the same. I am working on Windows. Can someone help me with this ?
Typically the default url rule for modules is module/controller/actin, so for access actionIndex inside CarsController inside CarsModule, your url should be localhost/cars/cars/index, not localhost/cars/index. If you don't like the url be like localhost/cars/cars/index, you can write one url manager rule to map localhost/cars/cars/index into localhost/cars/index. Something like this:
'cars/index' => 'cars/cars/index'
I'm trying to create a custom button that clones a DataObject using the unclecheese/betterbuttons v.1.2 module.
Everything is working fine but at the end I would like to redirect the user to the newly created DataObject edit page instead of refresh/go back. How can I do this?
Here is my custom button code:
class GridFieldCloneBetterButton extends DataExtension {
private static $better_buttons_actions = array(
'clone_do'
);
public function updateBetterButtonsActions($actions) {
$actions->push(
BetterButtonCustomAction::create('clone_do', 'Clone')
->setSuccessMessage('Object cloned')
->setRedirectType(BetterButtonCustomAction::GOBACK)
);
return $actions;
}
public function clone_do() {
$current_record = $this->owner;
$clone = $this->owner->duplicate();
}
}
Maybe if I can access GridFieldDetailForm_ItemRequest from inside the DataExtension I can make this work, but I really don't know how to do it.