class EntityReferenceBehaviorHandlerTaxonomyIndex extends EntityReferenceBehaviorHandler {
public function access($field, $instance) {
if ($instance['entity_type'] != 'node' || $field['settings']['target_type'] != 'taxonomy_term') {
return;
}
if ($field['storage']['type'] !== 'field_sql_storage') {
return;
}
return config_get('taxonomy.settings', 'maintain_index_table');
}
public function entityPostInsert($entity_type, $entity, $field, $instance) {
if ($entity_type != 'node') {
return;
}
$this->buildNodeIndex($entity);
}
public function entityPostUpdate($entity_type, $entity, $field, $instance) {
if ($entity_type != 'node') {
return;
}
$this->buildNodeIndex($entity);
}
protected function buildNodeIndex($node) {
$status = NULL;
if (config_get('taxonomy.settings', 'maintain_index_table')) {
if (!empty($node->original)) {
$status = (int) (!empty($node->status) || (!isset($node->status) && !empty($node->original->status)));
$sticky = (int) (!empty($node->sticky) || (!isset($node->sticky) && !empty($node->original->sticky)));
}
else {
$status = (int) (!empty($node->status));
$sticky = (int) (!empty($node->sticky));
}
}
if ($status) {
$tid_all = array();
foreach (field_info_instances('node', $node->type) as $instance) {
$field_name = $instance['field_name'];
$field = field_info_field($field_name);
if (!empty($field['settings']['target_type']) && $field['settings']['target_type'] == 'taxonomy_term' && $field['storage']['type'] == 'field_sql_storage') {
if (isset($node->{$field_name})) {
$items = $node->{$field_name};
}
elseif (isset($node->original->{$field_name})) {
$items = $node->original->{$field_name};
}
else {
continue;
}
foreach (field_available_languages('node', $field) as $langcode) {
if (!empty($items[$langcode])) {
foreach ($items[$langcode] as $item) {
$tid_all[$item['target_id']] = $item['target_id'];
}
}
}
}
$original_tid_all = array();
if ($field['module'] == 'taxonomy' && $field['storage']['type'] == 'field_sql_storage') {
if (isset($node->{$field_name})) {
$items = $node->{$field_name};
}
elseif (isset($node->original->{$field_name})) {
$items = $node->original->{$field_name};
}
else {
continue;
}
foreach (field_available_languages('node', $field) as $langcode) {
if (!empty($items[$langcode])) {
foreach ($items[$langcode] as $item) {
$original_tid_all[$item['tid']] = $item['tid'];
}
}
}
}
}
$tid_all = array_diff($tid_all, $original_tid_all);
if (!empty($tid_all)) {
foreach ($tid_all as $tid) {
$row = array(
'nid' => $node->nid,
'tid' => $tid,
'sticky' => $sticky,
'created' => $node->created,
);
$query = db_merge('taxonomy_index')
->key($row)
->fields($row);
$query->execute();
}
}
}
}
public function settingsForm($field, $instance) {
$form = array();
$target = $field['settings']['target_type'];
if ($target != 'taxonomy_term') {
$form['ti-on-terms'] = array(
'#markup' => t('This behavior can only be set when the target type is taxonomy_term, but the target of this field is %target.', array('%target' => $target)),
);
}
$entity_type = $instance['entity_type'];
if ($entity_type != 'node') {
$form['ti-on-nodes'] = array(
'#markup' => t('This behavior can only be set when the entity type is node, but the entity type of this instance is %type.', array('%type' => $entity_type)),
);
}
if (!config_get('taxonomy.settings', 'maintain_index_table')) {
$form['ti-disabled'] = array(
'#markup' => t('This core setting "maintain_index_table" is disabled.'),
);
}
return $form;
}
}