hook_block_view() in drupal 7 doesn't render - drupal-7

I have 2 servers production and development, my problem is with the modules, in occasional times my module render into developemnt but in production server don't render, I cleared the cache so many times, and debugging the module never pass for hook_block_view(),in the other hand, pass for menu_hook, and all de admin hooks, inclusive i can see the admin form for the module but the module's block didn't.
Has Someone a similar problem?
my code is below:
/**
* Implements hook_menu()
*/
function contenido_primario_menu () {
$items['admin/structure/contenido_primario'] = array(
'title' => t('Contenido Primario'),
'description' => t('Configurar imagenes y mapas'),
'page callback' => 'drupal_get_form',
'page arguments' => array('contenido_primario_configuration_form'),
'access arguments' => array('admnister contenido_primario'),
'file' => 'contenido_primario.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/structure/contenido_primario_modal'] = array(
'title' => t('Contenido Primario Modal'),
'description' => t('Configurar iconos correspondientes a la ventana modal'),
'page callback' => 'drupal_get_form',
'page arguments' => array('contenido_primario_modal_form'),
'file' => 'contenido_primario.modal.inc',
'access arguments' => array('admnister contenido_primario_modal'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_block_info()
*/
function contenido_primario_block_info() {
$bloque = array();
$bloque['contenido_primario_block'] = array(
'info' => t('Bloque de información, despligue de contenido primario'),
'cache' => DRUPAL_NO_CACHE,
);
return $bloque;
}
/**
* Implements hook_permission()
*/
function contenido_primario_permission() {
return array(
'admnister contenido_primario' => array(
'title' => t('Administrar Contenido Primario de la pagina Principal'),
'description' => t('Bloque de Administración para Contenido Primario'),
),
'admnister contenido_primario_modal' => array(
'title' => t('Administrar Contenido Primario de la pagina Principal'),
'description' => t('Bloque de Administración para Contenido Primario'),
),
);
}
/**
* Implements hook_contextual_links_view_alter()
*/
function contenido_primario_contextual_links_view_alter(&$element, $items) {
if(isset( $element['#element']['#block']->module) &&
$element['#element']['#block']->delta == 'contenido_primario_block') {
$element['#links']['contenido_primario'] = array(
'title' => t('Configurar Contenido Primario'),
'href' => 'admin/structure/contenido_primario',
);
$element['#links']['contenido_primario_modal'] = array(
'title' => t('Agregar iconos a ventana modal'),
'href' => 'admin/structure/contenido_primario_modal',
);
}
}
/**
* Implements hook_theme()
*/
function contenido_primario_theme($existing, $type, $theme, $path) {
return array(
'contenido_primario_template' => array(
'template' => 'view',
'path' => $path . '/templates',
),
);
}
/**
* Implements hook_theme()
*/
function contenido_primario_block_view($block_name = '') {
$block = array();
$icons = array();
//echo '<pre>';
if($block_name == 'contenido_primario_block' && current_path() == 'node') {
$module_path = base_path() . drupal_get_path('module', 'contenido_primario') . '/';
$files_path = base_path() .
variable_get('file_public_path', conf_path() . '/files') . '/contenido_primario/';
$icons_path =base_path() .
variable_get('file_public_path', conf_path() . '/files') . '/icons/';
//$list = module_list();
$background_color = variable_get('contenido_primario_modal_background', '');
// Get all available slides
//variable_set('contenido_primario_banner_settings', array());
$slides = variable_get('contenido_primario_banner_settings', array());
$modal_icons = variable_get('contenido_primario_icons_settings', array());
if(is_array($slides) && count($slides) > 0) {
$icons = array();
// weight, published, delete, fid, title, url
foreach ($slides as $key => $value) {
$alias = empty($value['url']) ? '' : $value['url'];
$file = file_load($value['fid_map']);
if (!empty($file)) {
$map_filename = $file->filename;
$image_map = _generate_image($file);
}
$file = file_load($value['fid_image']);
if (!empty($file)) {
$place_filename = $file->filename;
$image_place = _generate_image($file);
}
$titlecode = str_replace(' ', '-', $value['title']);
$titlecode = strtolower($titlecode);
$_placeurl = (!isset($value['url']) && empty($value['url'])) ? '': $value['url'];
$icon = array(
'path' => file_create_url($file->uri),
'title' => $value['title'],
'titlecode' => $titlecode,
'description' => $value['description']['value'],
'published' => $value['published'],
'map_image' => $image_map,
'place_image' => $image_place,
'place_filename' => $place_filename,
'map_filename' => $map_filename,
'url_place' => $_placeurl,
'alias' => $alias,
);
$icons[] = $icon;
}
}
$theme_args = array(
'background_color' => $background_color,
'icons_path' => $icons_path,
'files_path' => $files_path,
'module_path' => $module_path,
'modal_icons' => $modal_icons,
'icons' => $icons,
'active' => 'active',
);
#db_query("DELETE FROM {cache};");
$content = theme('contenido_primario_template', $theme_args);
$block = array(
'content' => $content,
);
}
return $block;
}
Is strange these problems between my develop and production server.

Related

how to get the submitted value of tableselect in drupal 7

function test($form, &$form_state){
$form = array();
$header = array(.............);
$values = array(.............);
$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#multiple' => $IsCheckbox,
'#empty' => t('No users found'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
} // end of function test()
function test_submit($form, &$form_state){
$selected = $form_state['values']['table'];
drupal_set_message($selected) // displays array index (0,1,2 etc)
return;
}
How to get the selected table row values in Drupal form. Need assistance on the issue. Any help would be appreciated.
What you get in your $selected is the index of $rows that you have selected in your table. To get the values in $rows you need to use the index that you have in $selected.
I created an easy example how to do it here:
function test($form, &$form_state)
{
$form = array();
$header = array(
'first_name' => t('First Name'),
'last_name' => t('Last Name'),
);
$rows = array(
// These are the index you get in submit function. The index could be some unique $key in database.
'1' => array('first_name' => 'Mario', 'last_name' => 'Mario'),
'2' => array('first_name' => 'Luigi', 'last_name' => 'Mario'),
'3' => array('first_name' => 'Princess Peach', 'last_name' => 'Toadstool'),
);
$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#multiple' => true,
'#empty' => t('No users found'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
} // end of function test()
function test_submit($form, &$form_state)
{
// This function should not be duplicated like this but It was easier to do.
$rows = array(
'1' => array('first_name' => 'Mario', 'last_name' => 'Mario'),
'2' => array('first_name' => 'Luigi', 'last_name' => 'Mario'),
'3' => array('first_name' => 'Princess Peach', 'last_name' => 'Toadstool'),
);
$names = array();
// Remove the names that has not been checked
$selected_names = array_filter($form_state['values']['table']);
// Iterate over the indexes that was selected to get the data from original array
foreach ($selected_names as $index ) {
array_push($names, $rows[$index]);
}
foreach($names as $name) {
drupal_set_message($name['first_name'] . ' ' . $name['last_name']);
}
}

Couldn't find Aco node identified by "Array ( [Aco0.model] => model [Aco0.foreign_key] => U ) "

I'm going through the following links to use Acl component in my application
http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html
http://code.tutsplus.com/tutorials/how-to-use-cakephps-access-control-lists--net-1345
in usersController i have a function to install aros and acos
public function install(){
$aro = $this->Acl->Aro;
$aco = $this->Acl->Aco;
$aro_groups = array(
0 => array(
'alias' => 'admin'
),
1 => array(
'alias' => 'operator'
),
2 => array(
'alias' => 'user'
),
);
$aco_groups = array(
0 => array(
'alias' => 'User'
),
1 => array(
'alias' => 'Supplier'
),
2 => array(
'alias' => 'Inventory'
),
3 => array(
'alias' => 'Invoice'
),
4 => array(
'alias' => 'Incentive'
),
5 => array(
'alias' => 'Promotion'
),
6 => array(
'alias' => 'Feedback'
),
7 => array(
'alias' => 'Message'
),
8 => array(
'alias' => 'History'
),
);
foreach($aro_groups as $data):
$aro->create();
$aro->save($data);
endforeach;
foreach($aco_groups as $data):
$aco->create();
$aco->save($data);
endforeach;
foreach($aco_groups as $data):
$this->Acl->allow('admin',$data);
$this->Acl->allow('operator',$data);
endforeach;
}
my user model is as follows:
public $belongsTo = array(
'Role' => array(
'className' => 'Role',
'foreignKey' => 'role_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public function beforeSave($options = array()){
if(isset($this->data['User']['password']))
{
$this->data['User']['password']= AuthComponent::password($this->data['User']['password']);
}
}
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
public function bindNode($user) {
return array('model' => 'Role', 'foreign_key' => $user['User']['role_id']);
}
role model:
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'role_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
return null;
}
now, when I try to access /users/install just to check that all relations are successfully created or not, i'm getting this error
AclNode::node() - Couldn't find Aco node identified by "Array ( [Aco0.model] => model [Aco0.foreign_key] => U ) "
Warning (2): Illegal string offset 'id' [CORE\Cake\Model\AclNode.php, line 140]
In the parentNode function in your User model, you have group_id instead of role_id.
Replace all occurrences of group_id to role_id in your User model and you should be ok and also Replace Group with Role
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['role_id'])) {
$roleId = $this->data['User']['role_id'];
} else {
$roleId = $this->field('role_id');
}
if (!$roleId) {
return null;
} else {
return array('Role' => array('id' => $roleId));
}
}

Cakephp hashing password while changing it

I try to make hashing while changing password correctly , but i cant, always after changing password, it's hashing twice or i don;t know. Please tell me what am i doing wrong.
class User extends AppModel
{
var $validate = array(
'name' => array(
'length' => array(
'rule' => array('minLength', 5),
'message' => 'Please enter your full name (more than 5 chars)',
'required' => true,
),
),
'username' => array(
'length' => array(
'rule' => array('minLength', 5),
'message' => 'Must be more than 5 characters',
'required' => true,
),
'alphanum' => array(
'rule' => 'alphanumeric',
'message' => 'May only contain letters and numbers',
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'Already taken',
),
),
'email' => array(
'email' => array(
'rule' => 'email',
'message' => 'Must be a valid email address',
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'Already taken',
),
),
'password' => array(
'empty' => array(
'rule' => 'notEmpty',
'message' => 'Must not be blank',
'required' => true,
),
),
'password_confirm' => array(
'required' => array(
'rule' => array('equalToField', 'password', true),
'message' => 'The password you entered does not match',
),
'length' => array(
'rule' => array('between', 6, 20),
'message' => 'Use between 6 and 20 characters',
),
'empty' => array(
'rule' => 'notEmpty',
'message' => 'Must not be blank',
),
),
);
function equalToField($array, $field) {
return strcmp($this->data[$this->alias][key($array)], $this->data[$this->alias][$field]) == 0;
}
var $validateChangePassword = array(
'_import' => array('password', 'password_confirm'),
'password_old' => array(
'correct' => array(
'rule' => 'password_old',
'message' => 'Does not match',
'required' => true,
),
'empty' => array(
'rule' => 'notEmpty',
'message' => 'Must not be blank',
),
),
);
function useValidationRules($key)
{
$variable = 'validate' . $key;
$rules = $this->$variable;
if (isset($rules['_import'])) {
foreach ($rules['_import'] as $key) {
$rules[$key] = $this->validate[$key];
}
unset($rules['_import']);
}
$this->validate = $rules;
}
function password_old($data)
{
$password = $this->field('password',
array('User.id' => $this->id));
return $password ===
Security::hash($data['password_old'], null, true);
}
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this- >alias]['password']);
}
return true;
}
And then my controller:
class UsersController extends AppController
{
var $components = array('Email');
/**
* Account details page (change password)
*/
function account()
{
// Set User's ID in model which is needed for validation
$this->User->id = $this->Auth->user('id');
// Load the user (avoid populating $this->data)
$current_user = $this->User->findById($this->User->id);
$this->set('current_user', $current_user);
$this->User->useValidationRules('ChangePassword');
$this->User->validate['password_confirm']['compare']['rule'] =
array('equalToField', 'password', false);
$this->User->set($this->data);
if (!empty($this->data) && $this->User->validates()) {
$password = $this->Auth->password($this->data['User']['password']);
$this->User->saveField('password', $password);
$this->Session->setFlash('Your password has been updated');
$this->redirect(array('action' => 'account'));
}
}
/**
* Registration page for new users
*/
// function register()
// {
// if (!empty($this->data)) {
// $this->User->create();
// if ($this->User->save($this->data)) {
// $this->Session->setFlash(__('Your account has been created.', true));
// $this->redirect('/');
// } else {
// $this->Session->setFlash(__('Your account could not be created.', true));
// }
// }
// }
public function register(){
if($this->request->is('post')){
$this->User->create();
if($this->User->save($this->request->data)){
$this->Session->setFlash(__('Użytkownik został zapisany', 'success'));
$this->redirect(array('controller'=>'ads', 'action'=>'index'));
} else {
$this->Session->setFlash(__('Błąd zapisu'), 'error');
}
}
}
/**
* Log a user out
*/
function logout()
{
return $this->redirect($this->Auth->logout());
}
/**
* Ran directly after the Auth component has executed
*/
function login()
{
// Check for a successful login
if($this->request->is('post')){
if($this->Auth->login()){
$this->User->id = $this->Auth->user('id'); // zapisuje date logowania
$this->User->saveField('lastlogin', date(DATE_ATOM)); // zapisuje date logowania
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Nieprawidłowy login lub hasło'), 'error');
}
}
}
and the view:
echo $this->Form->create(array('action' => 'account'));
echo $this->Form->input('password_old', array('label' => 'Old password', 'type' => 'password', 'autocomplete' => 'off'));
echo $this->Form->input('password_confirm', array('label' => 'New password', 'type' => 'password', 'autocomplete' => 'off'));
echo $this->Form->input('password', array('label' => 'Re-enter new password', 'type' => 'password', 'autocomplete' => 'off'));
echo $this->Form->end('Update Password');
Change this line in account function in UsersController from
$password = $this->Auth->password($this->data['User']['password']);
to
$password = $this->data['User']['password'];
$this->Auth->password() do the same function as AuthComponent::password() in model.
So your password was hashing twice.

FilesSize validator in Model using factory

That's my model SiteDesign.php in Object\Model:
namespace Object\Model;
use Zend\Validator\StringLength;
use Zend\Validator\NotEmpty;
use Zend\Validator\File\Size;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class SiteDesign {
public $logo_filename;
public $background_image;
public $background_color;
public function exchangeArray($data) {
$this->logo_filename = (isset($data['logo_filename'])) ? $data['logo_filename'] : null;
$this->background_image = (isset($data['background_image'])) ? $data['background_image'] : null;
$this->background_color = (isset($data['background_color'])) ? $data['background_color'] : null;
}
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new \Exception('Not used');
}
public function getInputFilter() {
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'logo_filename',
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'FilesSize',
'options' => array(
'max' => 4 * 1024,
'messages' => array(
Size::TOO_BIG => 'Размерът на логото не може да надвишава 5MB.',
),
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'background_image',
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'FilesSize',
'options' => array(
'max' => 4 * 1024,
'messages' => array(
Size::TOO_BIG => 'Размерът на изображението за фон не може да надвишава 5MB.',
),
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'background_color',
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
but there's an error message:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for FilesSize
so is it possible to define a FilesSize validator directly in the model using factory or not? And is this the right way to do it? Thanks in advance.
As of Zend\Validator\ValidatorPluginManager (which is responsible of instantiating validators by name) the correct name is `"FileSize" (you got too many "s" in there).

pagination weird behavior with search filter in cakephp

i wish to have a search form with one textbox, 2 dropdowns and a radio button (in cakephp). But this is to be done with pagination. Search and pagination work fine separately but not together. when i select the filter criteria and click on search button, the results are displyed fine on 1st page, but if i click on any of the pagination links, the filter criteria are lost and pagination data is displayed without any filter. How do i solve this?
controller code:
private function _index() {
$this->genre_list();
$this->language_list();
$conditions = array();
debug($this->postConditions($this->data)); die;
if (!empty($this->request->data['Artist'])) {
foreach ($this->request->data['Artist'] as $name => $record) {
if (isset($record) && !empty($record)) {
if ($name == 'name') {
$conditions = array(
$this->modelClass->User. 'User.name LIKE' => '%' . $record . '%',
);
} else {
$conditions[$this->modelClass . '.' . $name] = $record;
}
}
}
};
$this->paginate = array(
'contain' => array (
'User' => array(
'City',// => array('name'),
'State',// => array('name'),
'Country',// => array('name'),
),
'Genre',// => array('name'),
'Language',// => array('language'),
),
'limit' => 3,
'conditions' => $conditions
);
$data = $this->paginate($this->modelClass);
//~ debug($data);
$this->set(compact('data'));
}
view:
<?php
echo $this->Form->create('Search', array(
'type' => 'file',
'inputDefaults' => array(
'format' => array(
'label', 'between', 'input', 'error', 'after'
)
),
'class' => 'form-horizontal'
));
echo $this->Form->input('Artist.name', array(
'div' => 'control-group',
'label' => array(
'class' => 'control-label',
'text' => 'Artist Name'
),
'between' => '<div class="controls">',
'after' => '</div>',
'placeholder' => 'Name',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'style' => 'color:#B94A48'
)
)
));
echo $this->Form->input('Artist.genre_id', array(
'div' => 'control-group',
'empty' => 'All',
'label' => array(
'class' => 'control-label',
'text' => 'Genre'
),
'between' => '<div class="controls">',
'after' => '</div>',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'style' => 'color:#B94A48'
)
)
));
echo $this->Form->input('Artist.language_id', array(
'div' => 'control-group',
'empty' => 'All',
'label' => array(
'class' => 'control-label',
'text' => 'Select Lanuage'
),
'between' => '<div class="controls">',
'after' => '</div>',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'style' => 'color:#B94A48'
)
)
));
?>
<?php echo $this->element('pagination');
edited code with session
private function _index() {
$this->genre_list();
$this->language_list();
$conditions = array();
//~ foreach($this->request->params['named'] as $key => $record) {
//~ debug($this->request->params['named']);
//~ $this->request->data['Search'][$key] = $record;
//~ }
if (!empty($this->request->params['named']['page'])) {
// use session data for conditions
$conditions = (array)$this->Session->read('_indexConditions');
} else {
// delete session data
$this->Session->delete('_indexConditions');
}
$conditions = array();
if (!empty($this->request->data)) {
// new search! use the data to make conditions,
// like you did, and save the conditions
//~ if (!empty($this->request->data['Artist'])) {
foreach ($this->request->data['Search'] as $name => $record) {
if (isset($record) && !empty($record)) {
if ($name == 'name') {
$conditions = array(
$this->modelClass->User. 'User.name LIKE' => '%' . $record . '%',
);
} else {
$conditions[$this->modelClass . '.' . $name] = $record;
}
}
}
//~ }
$this->Session->write('_indexConditions', $conditions);
}
$this->paginate = array(
'contain' => array (
'User' => array(
'City',
'State',
'Country',
),
'Genre',
'Language',
),
'limit' => 3,
'conditions' => $conditions
);
$data = $this->paginate('Artist');
$this->set(compact('data'));
}
The answer is pretty simple: save the search conditions in the session or cookie and use those conditions if new ones aren't sent.
For simplicity's sake, I've omitted much of your code. Something like this should work.
private function _index() {
// check if this is a pagination request without data
if (!empty($this->request->params['named']['page']) {
// use session data for conditions
$conditions = (array)$this->Session->read('_indexConditions');
} else {
// delete session data
$this->Session->delete('_indexConditions');
}
$conditions = array();
if (!empty($this->request->data)) {
// new search! use the data to make conditions,
// like you did, and save the conditions
$this->Session->write('_indexConditions', $conditions);
}
$this->paginate = array(
'conditions' => $conditions
);
$data = $this->paginate($this->modelClass);
$this->set(compact('data'));
}
It should be wrapped in a component. On one of my projects I have a component that automatically does this and replaces the controller data for a more automated process.
got the solution without using session
in controller:
if (!empty($this->request->data) || $this->request->params['named']) {
foreach($this->request->params['named'] as $key => $record) {
if($key!= 'page')
$this->request->data['Search'][$key] = $record;
}
foreach ($this->request->data['Search'] as $name => $record) {
if (isset($record) && !empty($record)) {
$this->request->params['named'][$name] = $record;
if ($name == 'name') {
$conditions[$this->{$this->modelClass}->User. 'User.name LIKE'] = '%' . $record . '%';
} else {
$conditions[$this->modelClass . '.' . $name] = $record;
}
}
}
}
and in view provide the controller and action explicitly:
echo $this->Form->create('Search', array(
'type' => 'file',
'inputDefaults' => array(
'format' => array(
'label', 'between', 'input', 'error', 'after'
)
),
'url' => array(
'controller' => 'artists',
'action' => 'index',
),
'class' => 'form-horizontal'
));
solves the problem.
In 2.x, you can set your form to use GET instead of POST.
Change the type of your form from file to GET (BTW, why you use file??)
echo $this->Form->create('Search', array(
'type' => 'get',
And of course, instead of using the data array from Request
$this->request->data['Search']['name']
you will use the query array like this:
if (!empty($this->request->query['name'])) {

Resources