How can I assign an id and class with $this->Html->link(..);? - cakephp

echo $this->Html->link(
'more',
array(
'controller'=>'posts',
'action'=>'view',
$post['Post']['id']
)
);
How can I assign an id and class for this anchor/link? I want to override its css rule.

HTML attributes can be specified in an array as the third parameter.
echo $this->Html->link(
'more',
array(
'controller'=>'posts',
'action'=>'view',
$post['Post']['id']
),
array(
'id' => 'myId',
'class' => 'myClass'
)
);
More info in the Cookbook 2.x for the 2.x version or Cookbook 1.3 for CakePHP 1.3.

Related

cakephp 2.x pagination with custom ul

I need to generate html tags like the below .. to be precise, i need to add a class to the anchor tag added to the list, but i dnt seem to get it to work ..
<li class="myLIPrevclass"></li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li class="myLINextclass"></li>
The challenge is,
i cant seem to get a class across to the a tag in the next and the previous, i need to solve this
Even if i leave the a tag blank for the previous and next, CakePHP goes ahead and adds a '>' or '<' which in my case I do not need.
This is what I have tried ..
<?php
echo $this->Paginator->prev(
__(''),
array(
'tag' => 'li',
'class' => 'previous',
),
__(''),
array(
'tag' => 'li',
'class' => ''
)
);
echo $this->Paginator->numbers(
array(
'separator' => '',
'tag' => 'li',
'currentClass' => 'active',
'currentTag' => 'a'
)
);
echo $this->Paginator->next(
__(''),
array(
'tag' => 'li',
'class' => 'next',
),
__(''),
array(
'tag' => 'li',
'class' => ''
)
);
?>
For the disabled titles (for both previous and next) even if i pass in null, Cake add a '>' or '<', and I don't want that
I have checked
Creating Pagination With CakePHP For Custom Template Links
and
Add class to pagination links at CakePHP 2.2
My Cake Version is 2.4.x
I'll appreciate any pointers or help ..
Thanks

CakePHP inputDefaults and rewrite by field

I've created a large form in Cake and set default options via inputDefaults. However I wish to change the default values for an individual field.
In setting the form defaults, I wrote approximately this:
'inputDefaults' => array(
'error' => array(
'attributes' => array(
'wrap' => 'span',
'class' => 'invalidate column-7 offset-3')));
...with the result that all like fields produce the same error message. But, when I attempt to change the defaults for a single field, like so:
echo $this->Form->input('name', array(
'error' => array(
'attributes' => array(
'wrap' => 'span',
'class' => 'invalidate column-10'))));
It doesn't work. The field name produces an error whose class reads column-7 and offset-3, whereas I'd intended column-10.
Anybody know a solution?
$options['inputDefaults'] You can declare a set of default options for input() with the inputDefaults key to customize your default input creation:
echo $this->Form->create('User', array(
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
All inputs created from that point forward would inherit the options declared in inputDefaults. You can override the defaultOptions by declaring the option in the input() call:
echo $this->Form->input('password'); // No div, no label
// has a label element
echo $this->Form->input(
'username',
array('label' => 'Username')
);

comment plugin for cake2.0

I am using cakephp2.0 and want to integrate comment plugin but i got nothing .I was using commentDc plugin but its not working as my requirements.Because i am integreating my users login system with xenforo and commentDc plugin use Auth component so its not working properly.
Please let me know is there any simple comment plugin which i can integrate and modify as my needs.
Thanks,
Here's how I set up comments:
Comments table fields:
id
parent_type, matches the model name of the parent
parent_id
content
user_id, the sender
In any model that you want to be commentable, at this your associations:
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'parent_id',
'conditions' => array('Comment.parent_type' => 'question')
)
);
This is a view element:
<?php
/*
set variables:
$data : data of the parent
$type : the type of the parent
*/
if(!isset($name)) {
$name = 0;
}
foreach($data['Comment'] as $comment){
echo '<div class="comment">'.$comment['content'].
' - '.$this->Html->link($comment['User']['username'],array('controller'=>'users','action'=>'view',$comment['User']['id']))
.'</div>';
}
echo $this->Form->create(null, array('url' => '/comments/add','id'=>'qCommentForm'));
echo $this->Form->input('Comment.parent_id', array('type'=>'hidden','value'=>$data[$type]['id']));
echo $this->Form->input('Comment.parent_type', array('type'=>'hidden','value'=>$type));
echo $this->Form->textarea('Comment.content',array('div'=>'false','class'=>'small','label'=>false));
echo $this->Form->submit(__('Leave comment'),array('div'=>'false','class'=>'small'));
echo $this->Form->end();
?>
Then, in the view view for your model, add this (assuming you named the element comment.ctp:
<?php echo $this->element('comment',array('data'=>$modelData,'type'=>'MyModel')) ?>

Creating 'select' listboxes using FormHelper in CakePHP

I have two models, Category and Point. The associations are defined as:
Category hasMany Point
Point belongsTo Category
I would like, when adding Points to my database, to be able to select the category it belongs to from a <select> box, along with the rest of the form data.
Where would I need to set the category list and how could I do it? And how would I produce the select box?
I assume it could be done with
$form->input('categorieslist',array('type'=>'select')); //categorieslist needs
//setting somewhere.
Also to generalize a bit:
In a View with access to the Form helper
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
));
?>
The above will render a select input with two options. You can also place an empty option as the first item. Passing a value of true will simply append an empty option with a blank value to the beginning of the options rendered in the HTML.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
'empty' => true,
));
?>
You can pass a string to the 'empty' key to have it display custom text as the key field for the empty option.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'California' => 'CA',
'Oregon' => 'OR',
),
'empty' => 'choose a state',
));
?>
One last example, you can also pre-select an option with the selected key. The value should match the value of one of the select options, not the key.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'California' => 'CA',
'Oregon' => 'OR',
),
'empty' => 'choose a state',
'selected' => 'California',
));
?>
From the Model
Model->find( 'list', array( ... )); will always return an array formatted for use with select box options. If you pass data to your view stored in a variable with a lowercase plural model name, that is, ( $this->set( 'categories', $categories );, then you will automagically generate drop downs for related models by using the form helper in the view and passing it a data index of the same model name in singular form suffixed with "_id".
Aziz's answer at #2 is the example of that automagic kicking in.
CakePHP 1.3 Form Helper
CakePHP1.2 Form Helper
In the controller:
$categories = $this->Point->Category->find('list');
$this->set(compact('categories'));
In the view:
$form->input('category_id',array('type'=>'select'));

How do you style the paginator used in CakePHP with CSS?

How do you style the paginator with CSS used in CakePHP?
I can't find a way to attach a CSS class / ID to each of the "span" generated by the default pagination helper in CakePHP.
see: http://www.switchonthecode.com/tutorials/cakephp-part-6-pagination
and https://book.cakephp.org/3/en/views/helpers/paginator.html
What stands out here is that you can pass options to next(), prev(), and numbers()
what you want to do is pass the class option.
e.g.
$paginator->prev(
'<< Previous',
array(
'class' => 'PrevPg'
),
null,
array(
'class' => 'PrevPg DisabledPgLk'
)
).
$paginator->numbers(
array(
'class' => 'numbers'
)
).
$paginator->next(
'Next >>',
array(
'class' => 'NextPg'
),
null,
array(
'class' => 'NextPg DisabledPgLk'
)
),
array(
'style' => 'width: 100%;'
)

Resources