Skip to content

Instantly share code, notes, and snippets.

@tenken
Created May 23, 2017 16:26
Show Gist options
  • Save tenken/228395517c4cdcb65ffdb1ecaff6ebc8 to your computer and use it in GitHub Desktop.
Save tenken/228395517c4cdcb65ffdb1ecaff6ebc8 to your computer and use it in GitHub Desktop.
Example Prototype of fast item selection using VBO
<?php
// This example code is for D7. It uses a single hardcoded queue named essentially "workqueue_001".
// A relatively simple revision to this code may add a vbo_configurable form to the Action defintion
// to support any previously created EntityQueue as the list storage mechanism. Although in VBO-3.4
// this vbo_configurable attempt is breaking for me with a search_api based View display. Meh.
function prez_bulkadmin_action_info() {
return array(
'prez_bulkadmin_add_staff_work_queue_01' => array(
'type' => 'node',
'label' => t('Add to Staff Work Queue 01'),
'aggregate' => FALSE,
'pass rows' => FALSE,
'configurable' => FALSE,
// 'vbo_configurable' => TRUE,
// 'triggers' => array('any'),
// when using the Changed Poperty of VBO it will update the ts of the
// field. By omitting 'behavior' it will not fire a presave event.
// http://drupal.stackexchange.com/a/214833/3279
'behavior' => array(),
);
}
// Alter our particular admin content lookup form with a VBO field action "Add to Staff Work Queue 01"
function prez_bulkadmin_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'views_form_documents_missing_categories_page' && isset($form['confirm'])) {
// We override the default views handler here so that we can do a simpler
// SELECT INTO query instead of an enqueueing approach.
$form['actions']['submit']['#submit'] = array('smw_views_bulk_operations_form_submit');
}
}
/**
* Override Submit handler for all steps of the VBO multistep form.
* This is a copy of the views_bulk_operations_form_submit but modified to place items
* (quickly) into an EntityQueue based list in D7 using just the DB.
*/
function smw_views_bulk_operations_form_submit($form, &$form_state) {
#dpm($form_state);
$vbo = _views_bulk_operations_get_field($form_state['build_info']['args'][0]);
$entity_type = $vbo->get_entity_type();
switch ($form_state['step']) {
case 'views_form_views_form':
$form_state['selection'] = _views_bulk_operations_get_selection($vbo, $form_state);
$form_state['select_all_pages'] = $form_state['values']['select_all'];
$options = $vbo->get_operation_options($form_state['values']['operation']);
$form_state['operation'] = $operation = views_bulk_operations_get_operation($form_state['values']['operation'], $entity_type, $options);
if (!$operation->configurable() && $operation->getAdminOption('skip_confirmation')) {
break; // Go directly to execution
}
$form_state['step'] = $operation->configurable() ? 'views_bulk_operations_config_form' : 'views_bulk_operations_confirm_form';
$form_state['rebuild'] = TRUE;
return;
case 'views_bulk_operations_config_form':
$form_state['step'] = 'views_bulk_operations_confirm_form';
$operation = &$form_state['operation'];
$operation->formSubmit($form, $form_state);
if ($operation->getAdminOption('skip_confirmation')) {
break; // Go directly to execution
}
$form_state['rebuild'] = TRUE;
return;
case 'views_bulk_operations_confirm_form':
// SMW: This doesnt return it just breaks to the code after the switch.
break;
}
// Execute the operation.
# views_bulk_operations_execute($vbo, $form_state['operation'], $form_state['selection'], $form_state['select_all_pages']);
// if the user selected all pages, we unset all offsets and orderbys.
$keep_orderby = $vbo->query->orderby;
if ($form_state['select_all_pages']) {
unset($vbo->query->limit);
unset($vbo->query->offset);
// There is no orderby, this causes a Notice.
unset($vbo->query->orderby);
}
//
// Attempt to modify the Views select which includes NIDs for example to be
// like the schema the entityqueue_node field table expects.
//
// This table has the following columns:
// entity_type, bundle, deleted, entity_id, revision_id, language, delta, eq_node_target_id
//
// Exmaple how to add a row counter via SQL
// http://stackoverflow.com/a/3127004/1491507
$entity_type = 'entityqueue_subqueue';
$bundle = 'staff_work_queue_01';
$deleted = 0;
$language = 'und';
$entity_id = $revision_id = 8;
$hardcoded_view_unwanted_fields = array('field_data_field_docs_start_date_time');
// Remove un-necessary fields.
foreach ($hardcoded_view_unwanted_fields as $field_name) {
unset($vbo->query->table_queue[$field_name]);
unset($vbo->query->tables[$field_name]);
unset($vbo->query->fields[$field_name]);
}
// Wipe out all fields but 'nid'. Expressions will add the rest.
$backup_nid = $vbo->query->fields['nid'];
unset($vbo->query->fields);
$vbo->query->fields['nid'] = $backup_nid;
$vbo->query->fields['nid']['alias'] = 'eq_node_target_id';
// Assign missing fields to mimic en_node field table.
// This gets a SelectQuery object.
db_query("SET @curRow := 0");
$select_query = $vbo->query->query();
// These embedded ticks are not a typo. They provide the string in sql.
$select_query->addExpression("'$entity_type'", 'entity_type');
$select_query->addExpression("'$bundle'", 'bundle');
$select_query->addExpression($deleted, 'deleted');
$select_query->addExpression("'$language'", 'language');
$select_query->addExpression($entity_id, 'entity_id');
$select_query->addExpression($revision_id, 'revision_id');
// http://stackoverflow.com/a/3127004/1491507
$select_query->addExpression("@curRow := @curRow + 1", 'delta');
//$select_query->join('(SELECT @curRow := 0)', 'r');
# dpm($form_state['selection'], 'vbo selection');
# dpm($form_state['select_all_pages'], 'vbo select all');
// Get the View query.
// http://drupal.stackexchange.com/a/35901/3279
#$select_query = $vbo->query->query();
$args = $select_query->getArguments();
#dpm($vbo->query, 'select query object');
$sql = (string) $select_query;
#dpm($sql, 'the query Views ran');
#dpm($select_query->getArguments(), 'query args');
$temp_table_name = db_query_temporary($select_query, $args);
#dpm(db_select($temp_table_name)
# ->countQuery()
# ->execute()
# ->fetchField(), 'count of temp table items.');
db_delete('field_data_eq_node')->condition('bundle', $bundle, '=')->execute();
db_query("INSERT INTO field_data_eq_node (entity_type, bundle, deleted, entity_id, revision_id, language, delta, eq_node_target_id)
SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, eq_node_target_id FROM $temp_table_name");
$vbo->query->orderby = $keep_orderby;
// Redirect.
$query = drupal_get_query_parameters($_GET, array('q'));
$form_state['redirect'] = array(
'path' => $vbo->view->get_url(),
array('query' => $query)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment