custom admin module edit data not working properly - php

I am creating custom admin module where grid, edit, new, delete work as same as product grid. Everything working fine but when i click on grid row to edit item it redirects correctly but the all pages value is blank.
If you need any page please ask me here i can post the pages here

Normally in edit pages there is a form container, which contains a form, which has a method something like this:
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$fieldset = $form->addFieldset(/* ...stuff... */);
$fieldset->addField(/* ...much more stuff... */);
$form->setUseContainer(true);
$this->setForm($form);
}
Without seeing any code I cannot guess what you have named things but at some point after adding fields you must also do:
$model = Mage::registry('some model you registered in a controller');
$form->setValues($model);

Related

silverstripe 4 - Render UserDefinedForm on custom page templates

I need some help with converting SS3 to SS4. I would like to render my contact form on another page as well as my default Contact page. I managed to get it working in SS3 but things are a little different in SS4 and I am not sure how to write the function or where to put it. I have tried a bunch of combinations and locations but I need help.
In SS3 I created my UserDefineForm page with its fields. I then added the following to the custom page that I wanted the form to render too:
class IndexPage_Controller extends Page_Controller {
// Sign up form
public function SignupForm(){
$get = DataObject::get_one('SiteTree', "URLSegment = 'contact-me'");
return new UserDefinedForm_Controller($get);
}
}
What/Where do I put the function in SS4 to get the form fields to render on the custom page template as it does on the Contact us page?
Thank you in advance.
The code below should work.
public function getSignupForm()
{
$page = \SilverStripe\UserForms\Model\UserDefinedForm::get()->filter('URLSegment', 'contact-me')->first();
$controller = \SilverStripe\UserForms\Control\UserDefinedFormController::create($page);
return $controller->Form();
}

Silverstripe dynamic page types

Am litle confused with new project. Am working site for one courses school.
Course page:
Courses page is added via administration admin/pages/ and have own page type and data object. Course, CoursePage, CourseCategory. CoursePage has page model and controller where i have few methods for listing courses and categories. Also i have template CoursePage.ss and CoursePage_details.ss.
All courses has one course category.
Inside CoursePage_Controller i have two main methods:
public function details(SS_HTTPRequest $request)
public function ListAllCourses() {
return Course::get();
}
Adding new courses:
Adding new courses is done via admin model. I have CourseAdmin which extends ModelAdmin and all work good.
Displaying courses:
This is done via Course_Controller and Course_Page.ss and Course_Page_details.ss
Everything work fantastic but now problem is comming:
Problem:
My client want to create child pages inside Courses page and assign selected courses on a specific page.
So he want to create new child Web development courses page and want to loop only courses for web development on that page.
Here i have problem becouse am adding courses via admin model. Next, all that sub page must have own page type like WebDevelopmentPage where i will query and filter that courses by category.
So if client want to add new child page and want to assign courses to that child page he must call me becouse i must create new page type and new page template for all that page, for that new child page! That is realy bad...
if there is a way that I could to filter the information through subpages? Or any trick to system automticly create new page type?
Update:
Solution 1:
I try this and work ok, but i think this is a bad practice.
Inside Course data object i add new db field UrlSegment where user must map some url segment like on page url. With what i will filter all on one page
public function Courses()
{
if($this->URLSegment == "all-courses") {
return Courses::get();
}else {
$c = Courses::get()->filter(array(
'UrlSegment' => $this->URLSegment
))->sort('Featured', 'DESC');
return $c;
}
}

Using renderView() in a custom ModuleAdminController (PS1.6)

I am trying to add the view action to my back office module page but i cannot display anything with the renderview() function. I can already display my list with renderList() and it's working well. I also tried renderForm() and it works well too but it seemz i can't get renderView() to display something.
public function renderView(){
if(!($config = $this->loadObject())){
return;
}
$data = Config::getDataForm(Tools::getValue('id_config'));
// var_dump($data);
$this->tpl_view_vars = array(
'id_config' => $data['id_config'],
'prix' => $data['prix'],
'hauteur' => $data['hauteur_passage']
);
return parent::renderView();
}
This is a pretty basic code. My getDataForm($id_config) is getting fields from database in an array so that i can display it. I can see the var_dump displaying for a short time before displaying the blank page with prestashop header and footer. I tried to see if i was doing something wrond by checking other AdminController such as AdminCartsController or AdminCustomersController but it seems that their renderView() function is more or less written the same way.
Thanks in advance for your help !
I manage to resolve this problem simply by adding a view tpl in /modules/mymodule/views/templates/admin/mymodule/helpers/view/.
I wrongly assumed that it didn't need to create a template file for the view action as it didn't need one for the list and form action. After searching through modules and admin files i managed to find that there were indeed a custom view.tpl for the view action.
The renderview() method lets you set up the variables you want to use in your view.tpl.
For more information on how it works, you can check AdminCustomersController to see how it is on the controller side and /adminxxxx/themes/default/template/controllers/customers/helpers/view/view.tpl to see how the template is written.
Feel free to edit or comment if you need more information
If you want to add a configuration page on your module, you'll have to add this function to your module :
public function getContent()
{
// return some html content
}
If you want to use a controller, then you'll have to create a Controller that extends ModuleAdminController and add it in the tabs of the back-office.

Display Prestashop new product field on product-list.tpl and home featured modules

Using this tutorial on this page Adding new tabs and fields to Prestashop products’ back office, I was able to add a new field for author for products in my Prestashop. I was also able to display it on product page by adding Product.php to override/classes/ and inserting this code below:
class Product extends ProductCore
{
/** #var string Custom Product Field */
public $custom_field;
}
I then added {$product->custom_field} to product.tpl to display the new field. My challenge is that the same code does not work when added to product-list.tpl and the homefeatured.tpl module files.
Can any one explain how to achieve this? I am not an expert but I can find my way around tutorials if I have one. Thanks!
Find the function used by the module Homefeatured to get the products and edit the SQL request in this function to add your new field.
You can't display your new propertie because the SQL request don't get it.
Use . instead of ->
In product-list.tpl & homefeatured.tpl $procuct is array not object.
And do not miss the getFields() method in Product class:
public function getFields()
{
$fields = parent::getFields();
$fields['custom_field'] = $this->custom_field;
return $fields;
}

Admin form with "extra" fields

I have a form for an object called AccountImport. This form lives in an admin-generated module. In addition to the fields that map directly to this object's attributes, I need a couple extra fields.
If I just add the fields to the AccountImport form, it won't save correctly because the form will no longer match the AccountImport object.
If I create a template manually and splice the extra fields in that way, I'm throwing away all the stuff the admin generator gives me for free (i.e. formatting, "Back to list" button, save buttons).
What's a "good" way to do what I'm trying to do?
If you define additional fields in generator.yml, you can override one of the admin generator actions to handle the fields however you want.
Look at the generated actions.class.php in cache/YOURAPP/YOURENV/modules/autoYOURMODULE/actions/actions.class.php . You can override any of those functions with your own in apps/YOURAPP/modules/YOURMODULE/actions/actions.class.php, because it inherits from that cached file. When you make changes to generator.conf, the cached file is updated but your code will still override it. You probably want to override processForm().
I have an example of this in step 5 at this blog post:
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid())
{
$notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';
// NEW: deal with tags
if ($form->getValue('remove_tags')) {
foreach (preg_split('/\s*,\s*/', $form->getValue('remove_tags')) as $tag) {
$form->getObject()->removeTag($tag);
}
}
if ($form->getValue('new_tags')) {
foreach (preg_split('/\s*,\s*/', $form->getValue('new_tags')) as $tag) {
// sorry, it would be better to not hard-code this string
if ($tag == 'Add tags with commas') continue;
$form->getObject()->addTag($tag);
}
}
try {
$complaint = $form->save();
// and the remainder is just pasted from the generated actions file
When I realized I could read the generated files in the cache to see exactly what the admin generator was doing, and that I could override any part of them, it made me a lot more productive with the admin generator.
I asume you have added the extra fields as widgets to your form object, but have you also added their validators?
No matter which form fields you include in the form object, as long as the generator.yml file doesn't override the settings of the form (ie you don't set any value for the [new|form|edit].display key in that file) the object should get successfully saved on valid input.

Categories