how to set default theme in twig? - twig

I use a silex with twig;
I create custom form_div_layout and put it in the webroot( for example)
I register TwigService provider like this
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/../views',
'twig.options' => array(
'cache' => __DIR__ . '/../cache/twig',
'strict_variables' => false
),
'twig.form.templates'=> [WEBROOT . '/form_div_layout.twig']
));
but i have an error
Twig_Error_Loader: Template "/home/versh/sale/web/form_div_layout.twig" is not defined () in "layout.twig" at line 52.
how to register theme correctly ?
I know that if i put theme in the twig.path it will work, but this is not solution

I'm using Twig with namespaces and I think it is the most flexible practice:
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.options' => array(
'cache' => true,
'strict_variables' => true,
'debug' => false,
'autoescape' => true
)
));
// set namespace for your application
$app['twig.loader.filesystem']->addPath(WHERE_EVER_YOU_WANT_PATH, 'yourApplication');
Now you can render templates using the namespace:
return $app['twig']->render('#yourApplication/sample.twig', array());
you can define as many namespaces as you need.

You have to add base template form_div_layout.html.twig to twig.form.templates option too.
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/../views',
'twig.options'=>array(
'cache' => __DIR__.'/../cache',
),
'twig.form.templates' => array(
'form_div_layout.html.twig',
'theme/form_div_layout.twig'
),
));

Related

How to set sort order while add attribute programmatically

I want to set sort order in attribute. In mysql file i m used this
$installer->addAttribute('customer','badge', array(
'label' => 'Badge',
'type' => 'text', //backend_type
'input' => 'multiselect', //frontend_input
'backend' => 'eav/entity_attribute_backend_array',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'source' => 'marketplace/eav_entity_attribute_source_badge', // Goes to Step 2
'visible' => true,
'required' => false,
'default' => '',
'frontend' => '',
'unique' => false,
'note' => '',
'sort_order' => 10
));
Mage::getSingleton('eav/config')
->getAttribute('customer', 'badge')
->setData('used_in_forms', array('customer_account_create','customer_account_edit','customer_address_edit','checkout_onepage_register','checkout_onepage_register_guest','checkout_onepage_billing_address','adminhtml_customer','checkout_onepage_shipping_address','checkout_multishipping_register'))
->save();
but it not worked how to set sort order for this attribute
when you add attribute programmatically that time you have to set order like this
Mage::getSingleton('eav/config')
->getAttribute('customer', 'badge')
->setSortOrder(100)
->setData('used_in_forms', array('customer_account_create','customer_account_edit','customer_address_edit','checkout_onepage_register','checkout_onepage_register_guest','checkout_onepage_billing_address','adminhtml_customer','checkout_onepage_shipping_address','checkout_multishipping_register'))
->save();
'sort_order' is not worked
Or
You have to use position instead of sort_order.
'position' => 20
From Mage_Customer_Model_Resource_Setup
/**
* Prepare customer attribute values to save in additional table
*
* #param array $attr
* #return array
*/
protected function _prepareValues($attr)
{
$data = parent::_prepareValues($attr);
$data = array_merge($data, array(
'is_visible' => $this->_getValue($attr, 'visible', 1),
'is_system' => $this->_getValue($attr, 'system', 1),
'input_filter' => $this->_getValue($attr, 'input_filter', null),
'multiline_count' => $this->_getValue($attr, 'multiline_count', 0),
'validate_rules' => $this->_getValue($attr, 'validate_rules', null),
'data_model' => $this->_getValue($attr, 'data', null),
'sort_order' => $this->_getValue($attr, 'position', 0)
));
return $data;
}
You can create a new upgrade.php in your app/local/NAMESPACE/NAME/sql/NAME_setup folder with the code below:
<?php
// This installer scripts update a product attribute to Magento programmatically.
$model = Mage::getModel('catalog/resource_eav_attribute');
$model->loadByCode(4, 'color');
// Create attribute:
// We create a new installer class here so we can also use this snippet in a non-EAV setup script.
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->startSetup();
if ($model) {
$model->setPosition(3)
->save();
}
// Done:
$installer->endSetup();
And don't forget to update config.xml in app/local/NAMESPACE/NAME/etc/
folder with the code below:
<config>
<modules>
<Namespace_Name>
<version>0.0.2</version>
</Namespace_Name>
</modules>
<global>
<resources>
<name_setup>
<setup>
<module>Namespace_Name</module>
</setup>
</name_setup>
</resources>
</global>
</config>

Restrict sonata_type_model_list to 1 context

I am trying to allow users to select from existing images in the system and have context set like the provided example and this works. How can I stop the users from selecting from any other context type?
$form->add('image', 'sonata_type_model_list', array(), array('link_parameters' => array('context' => 'news')))
Not tested but I just stumbled over the parameter: hide_context
$form->add('image', 'sonata_type_model_list', array(), array(
'link_parameters' => array(
'context' => 'news',
'hide_context' => true
)
))
Maybe you want to give it a try?

installing 'EdpModuleLayouts' to zendframework 2

i am trying to install 'EdpModuleLayouts'
i have followed the instruction for the module here:
i also followed the issue here:
i. e; I placed the following code in my application.config.php?
return array(
'modules' => array(
... ,
'EdpModuleLayouts'
)
);
i then placed the following code in my module config
'view_manager' => array(
'template_path_stack' => array(
'workers' => __DIR__ . '/../view',
'Workers/layout' => __DIR__ . '/../view/layout/layout.phtml'
),
),
'module_layouts' => array(
'Workers' => 'Workers/layout'
),
i got the following error report:
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException'
with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render
template "Workers/layout"; resolver could not resolve to a file' in
zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php
on line 499
The error message is so specific.
Unable to render template "Workers/layout"
This means that there is no template Workers/layout defined within the view_manager configuration. And the trick here is that you made a simple mistake of placing the configuration at the wrong place.
template_path_stack is used to give the view_manager information about some folders where template files are mapped going by their file-path.
template_map is used to give the view_manager information about which file covers a specific template.
The difference of the two can easily be seen when checking the module.config.php of the ZendSkeletonApplication. With this in mind, all you have to do is to change your configuration.
Another hint: don't CamelCase configuration keys, keep them lowercased ;)
'view_manager' => array(
'template_path_stack' => array(
'workers' => __DIR__ . '/../view',
),
'template_map' => array(
// consider to make this lowercase "workers/layout"
'Workers/layout' => __DIR__ . '/../view/layout/layout.phtml'
)
),
'module_layouts' => array(
'Workers' => 'Workers/layout'
),

ZendFramework 2 - Error in loading layout for different modules

In ./config/application.config.php
return array(
'modules' => array(
'Application',
'Admin',
)
...
I have 2 separate set of layouts, ./module/Application/view/layout/layout.phtml and ./module/Admin/view/layout/layout.phtml
In ./module/Admin/config/module.config.php
...
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'header' => __DIR__ . '/../view/layout/header.phtml',
'footer' => __DIR__ . '/../view/layout/footer.phtml',
'paginator' => __DIR__ . '/../view/layout/paginator.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
)
...
In ./module/Application/config/module.config.php
...
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'header' => __DIR__ . '/../view/layout/header.phtml',
'footer' => __DIR__ . '/../view/layout/footer.phtml',
'paginator' => __DIR__ . '/../view/layout/paginator.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
)
...
Basically they are different set and some of the content are different. Unfortunately, both module only load the layout located in In ./module/Admin/config/module.config.php
I googled but didn't fount any solution that I want. Anyone has any idea on this?
You may be interested to know, what your configuration actually does. My Blog Post about this Topic, may interest you. Ultimately all Configuration files will be merged to one. The global configuration keys are not on a per-module basis ;)
To achieve your goal you should read Evan Courys Blog Post "Module-specific layouts in ZF2"
Evan provides a Module "EdpModuleLayouts" that makes things pretty easy. If however you only need one alternative Layout for your AdminModule, then i suggest you simply go with the example code of his Blog Post to set an alternate Layout for your AdminModule directy via your AdminModule/Module::onBootstrap
class Module
{
public function onBootstrap($e)
{
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
if ('AdminModule' === $moduleNamespace ) {
$controller->layout('layout/admin');
}
}, 100);
}
}
Not that this will set the layout to layout/admin. You would need to provide this key via your configuration:
'template_map' => array(
'layout/admin' => 'path/to/admin/module/view/layout/admin.phtml',
)

Drupal Profile alter: hook_form_alter

After reviewing some posts here and elsewhere, I still can't seem to manually add a select field to the profile. (I need the select list to be populated with a SQL query, not supported with core profile module.)
So far, I am trying two different ways: hook form alter ($form_id == 'user-register' & hook user ($op == 'register') -- but I can't even get the field to appear in the registration form.
function accountselect_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'register'){
$fields['account_select'] = array(
'#type' => 'fieldset',
'#title' => t('Your Security Question')
);
$fields['account_select']['account_name'] = array(
'#type' => 'select',
'#default_value' => 'Select',
'#description' => t('Select a verification question in case you forget your password'),
'#options' => array(t('Select One'),
t('Where you attended Elementry School'), t('Your Best Man'))
);
return $fields;
}
Here is the hook form alter attempt
function accountselect_form_alter(&$form, $form_state, $form_id){
if($form_id == 'user-register') {
$form['account_select']['account_name'] = array(
'#type' => 'select',
'#title' => t('Account'),
'#description' => t('Enter the account to which the contact belongs.'),
'#options' => array(t('Account1'),t('account2'), t('account3')),
'#default_value' => $edit ['Account']
);
}
return $fields;
}
Sorry Guys, the code here is correct. I did a little debugging when the module was first enabled. I thought I had successfully fixed the problem, but what really happened is that the module became disabled. So, no matter what was in there, it would have had no effect....
No worries, I've punched myself in the face for the stupid question....

Resources