Drupal Hook for taxonomy weight change - drupal-7

Is there any hook or way to execute a code when we change the taxonomy terms weight. E.g When we change create parent/child relationship between the terms.
function hook_taxonomy_term_update Doesn't seem to work for that.

This will do the job.
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'taxonomy_overview_terms':
if($form['#vocabulary']->machine_name == 'your_vocabulary_name'){
//adding a custom callback for after re-ordering
$form['#submit'][] = 'mymodule_custom_callback';
}
break;
}
}
function mymodule_custom_callback(){
//custom logic
}

Related

Make a list field editable when this field is a many_to_one type using in Sonata-project Symfony

My entity
/**
* #ORM\ManyToOne(targetEntity="Estat", inversedBy="temes")
*/
private $estat;
public function setEstat(\Ncd\ForumBundle\Entity\Estat $estat = null)
{
$this->estat = $estat;
return $this;
}
My admin
protected function configureListFields(ListMapper $listMapper)
{
//$estats=$this->getEstatsPossibles()->toArray();
$estats=array();
foreach($this->getEstatsPossibles() as $estat)
{
$estats[$estat->getId()]=$estat->getNom();
}
$listMapper
->add('estat', 'choice',['editable' => true,'choices'=> $estats])
I'd like to make estat field editable in the list grid. Doing it on this way I get make it editable, a combobox appears but when I chose an option I get an exception because setEstat function of my entity does not recive an Estat entity, but a string (the array's key).
Trying
->add('estat', 'many_to_one',['editable' => true,'choices'=> $estats])
Only appears a link to the entity without any possibility to change.
Is it possible?
Waiting for a better and cleaner solution I'v solved this injecting an entityManager in my entity following the solution of this answer:
Get entityManager inside an Entity
Then, in my entity I've changed setEstat function:
public function setEstat( $estat = null)
{
if (is_object($estat) && get_class($estat)=='Ncd\ForumBundle\Entity\Estat')
{
$this->estat=$estat;
} else {
$estat_o=$this->em->getRepository('Ncd\ForumBundle\Entity\Estat')->find((int)$estat);
$this->estat = $estat_o;
}
return $this;
}

cakephp validation to ensure null field

In my scenario I have a rest controller that I am validating input data.
I have built a form that I am using purely for custom validation that looks like this:
$validator
->requirePresence('sport_type')
->add('sport_type', 'Require fields missing', [
'rule' => function ($value) {
switch ($value) {
case 'Football':
$this->validator()
->requirePresence('football_id');
break;
case 'Basketball':
$this->validator()
->requirePresence('basketball_id');
break;
default:
return true;
}
return true;
}
])
->notEmpty('football_id')
->notEmpty('basketball_id')
return $validator;
What I need to be able to do, is in the default case, ensure that the other fields are null - if for example, sport_type = 'Motorsport', then the validator must return false if the input data DOES contain something in football_id or basketball_id.
I cannot see any kind of requireEmpty type of method in cake, so can anyone suggest how to accomplish this. Do I need a separate custom validator for that single rule, and how would I call it from this form validator ?

What is dispatcherIndex used for in React TODO example

All,
I just start learning React FLUX with Facebook TODO example Here
There is a field in TODO Store called dispatcherIndex, I wonder what that field used for?
Thanks
The dispatcherIndex is never invoked as you normally would expect from a function, i.e., TodoStore.dispatcherIndex().
But since the value of dispatcherIndex is a function call (no just a function, but a function call), that function call happens in the initialization of your object. So when the TodoStore is initialized, this code (the function call) is run:
AppDispatcher.register(function(payload) {
var action = payload.action;
var text;
switch(action.actionType) {
case TodoConstants.TODO_CREATE:
text = action.text.trim();
if (text !== '') {
create(text);
TodoStore.emitChange();
}
break;
case TodoConstants.TODO_DESTROY:
destroy(action.id);
TodoStore.emitChange();
break;
// add more cases for other actionTypes, like TODO_UPDATE, etc.
}
return true; // No errors. Needed by promise in Dispatcher.
})
You're calling AppDispatcher.register and passing it a callback function. This callback you are passing is not executed now, it is registered (added to the _callbacks array) to be called later, whenever you call one of the methods that calls AppDispatcher.handleViewAction, that are TodoActions.create and TodoActions.destroy.
In the code snippet in that page, they have included the tying up of the Dispatcher to the Store within the Store definition and that is what the dispatcherIndex is doing.
However, in the code that has been put on github, this is outside the store definition. In that code, Store definition starts at line 77
var TodoStore = assign({}, EventEmitter.prototype, {
and ends at line 117 after which you are registering the callback
removeChangeListener: function(callback) {
this.removeListener(CHANGE_EVENT, callback);
}
}); //Todo Store definition ends here
// Register callback to handle all updates
AppDispatcher.register(function(action) {
var text;
switch(action.actionType) {
case TodoConstants.TODO_CREATE:
text = action.text.trim();
if (text !== '') {
create(text);
TodoStore.emitChange();
}
break;
Basically, in Flux architecture, flow is unidirectional, so from the React components action is triggered and this action is then dispatched to those stores where the callback has been registered for that action.

callback functions for cakephp elements?

This might be a naive question since I am new to cakephp.
I am working on a project where there are many layouts, helpers and elements. Because this is a mutli-language site, I am intercepting the final rendered output to do some language conversion(so each visitor only sees his native language on the site including user input).
I've managed to convert most of the layouts and helpers by adding code in two places: AppController's afterFilter() and AppHeler's afterRender() function. But I can't figure out a centralized way to handle the elements and there are dozens of them.
So here are my questions: do all elements in cakephp have a common ancestor class? If so, does this ancestor class have callback functions like afterRender()?
Many thanks!
I'm not sure such a callback exists specific for 'elements', but looking at the source code, View::element() renders an element using the same _render() method as the View itself, and should trigger beforeRender() and afterRender()
Creating a custom View Class and Custom Callbacks
You may use a custom 'View' class and override the element() method, for example to have your own 'custom' callbacks being triggered in helpers
Something like this;
app/view/app_view.php
class AppViewView extends View {
/**
* custom 'element()' method, triggers custom
* before/aferRenderElement callbacks on all loaded helpers
*/
public function element($name, $params = array(), $loadHelpers = false)
{
$this->_triggerHelpers('beforeRenderElement');
$output = parent::element($name, $params, $loadHelpers);
$this->_triggerHelpers('afterRenderElement');
}
/**
* Names of custom callbacks
*/
protected $_customCallBacks = array(
'beforeRenderElement',
'afterRenderElement',
);
function _triggerHelpers($callback)
{
if (!in_array($callback, $this->_customCallbacks)) {
// it's a standard callback, let the parent class handle it
return parent::_triggerHelpers($callback);
}
if (empty($this->loaded)) {
return false;
}
$helpers = array_keys($this->loaded);
foreach ($helpers as $helperName) {
$helper =& $this->loaded[$helperName];
if (is_object($helper)) {
if (
is_subclass_of($helper, 'Helper')
&& method_exists($helper, $callback)
) {
$helper->{$callback}();
}
}
}
}
}
Then, in your AppController specify the 'view' class to use;
class AppController extends Controller {
public $view = 'AppView';
}

How to save data POSTed from From into database not using Form->setModel()

I know that's kinda simple and lame question, but still.
I have a Form which should not show all Model fields, but only some of them. That's why I can't use Form->setModel($m), because that'll automatically add all fields into Form.
So I add Model into page, then add form into page and then use form->importFields like this:
$m = $p->add('Model_Example');
$f = $p->add('Form');
//$f->setModel($m); // can't use this because that'll import all model fields
$f->importFields($m,array('id','description'));
$f->addSubmit('Save');
What I don't understand in this situation is - how to save this data in database, because $f->update() in onSubmit event will not work. Basically nothing I tried will work because Form have no associated Model (with setModel).
How about this way?
$your_form->setModel($model,array('name','email','age'));
I have solution for mixed form. Add custom form fields in form init and manipulate with them by hooks ('afterLoad','beforeSave')
In this case you can use setModel() method
$form->setModel('Some_Model',array('title','description'));
class Form_AddTask extends Form {
function init(){
parent::init();
$this->r=$this->addField('autocomplete/basic','contact');
$this->r->setModel('ContactEntity_My');
}
function setModel($model,$actual_fields=undefined){
parent::setModel($model,$actual_fields);
$this->model->addHook('afterLoad',array($this,'setContactId'));
$this->model->addHook('beforeSave',array($this,'setContactEntityId'));
return $this->model;
}
// set saved value for editing
function setContactId() {
$this->r->set($this->model->get('contact_entity_id'));
}
function setContactEntityId() {
$this->model->set('contact_entity_id',$this->get('contact'));
}
}
There is a hook 'validate' as well in Form_Basic::submitted(), so you can add
$this->addHook('validate',array($this,'validateCustomData'));
and validate your data in Form::validateCustomData()
Why not set the fields to hidden in the model?
I.e.:
class Model_Example extends Model_Table {
public $table='assessment';
function init() {
parent::init();
$grant->addField('hidden_field')->hidden(true);
}
}
And then:
$m = $p->add('Model_Example');
$f = $p->add('Form');
$f->setModel($m);

Resources