Skip to content

Instantly share code, notes, and snippets.

@pcambra
Last active October 14, 2015 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcambra/72b575d53d42ae22b0ce to your computer and use it in GitHub Desktop.
Save pcambra/72b575d53d42ae22b0ce to your computer and use it in GitHub Desktop.
Look for empty mandatory fields
<?php
/**
* Implements hook_menu().
*/
function {my_module}_menu() {
$items = array();
$items['check-mandatory-fields'] = array(
'title' => 'Mandatory content missing',
'page callback' => 'drupal_get_form',
'page arguments' => array('{my_module}_mcm'),
'access arguments' => array('access content'),
);
return $items;
}
function {my_module}_mcm() {
$form = array();
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function {my_module}_mcm_submit($form, &$form_state) {
drupal_set_message('Gathering the empty fields');
$mandatory_fields = array();
$excluded_fields = array('group_content_access', 'og_group_ref');
$instances = field_info_instances('node');
foreach ($instances as $bundle_name => $bundle) {
foreach ($bundle as $field_name => $field) {
if ($field['required'] == TRUE && !in_array($field_name, $excluded_fields)) {
$mandatory_fields[$bundle_name][] = $field_name;
}
}
}
$query = new EntityFieldQuery();
$results = $query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', array_keys($mandatory_fields), 'IN')
->propertyOrderBy('nid')
->execute();
$batch = array(
'operations' => array(
array('{my_module}_mcm_process_batch', array($results['node'], $mandatory_fields)),
),
'finished' => '{my_module}_mcm_process_batch_finished',
'title' => t('Missing required content'),
'init_message' => t('Batch is starting...'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Batch has encountered an error.')
);
batch_set($batch);
batch_process('check-mandatory-fields');
}
/**
* The batch processor.
*/
function {my_module}_mcm_process_batch($nodes, $fields, &$context) {
$limit = 1500;
$context['finished'] = 0;
if (!isset($context['sandbox']['file'])) {
$field_labels = array(
'Id',
'Title',
'Type',
'Created date',
'Operation(s)',
'URL',
'Author',
'Empty fields',
);
// Create the file and print the labels in the header row.
$filename = 'empty_required_fields.csv';
$file_path = file_directory_temp() . '/' . $filename;
$handle = fopen($file_path, 'w');
fputcsv($handle, $field_labels);
fclose($handle);
$context['sandbox']['nodes'] = $nodes;
$context['sandbox']['total_nodes'] = count($nodes);
$context['sandbox']['file'] = $file_path;
$context['results']['count'] = 0;
}
$handle = fopen($context['sandbox']['file'], 'a');
if ($nodes_pending = count($context['sandbox']['nodes'])) {
$actual_limit = min($nodes_pending, $limit);
$node_info = array_slice(
$context['sandbox']['nodes'],
$context['results']['count'],
$actual_limit,
TRUE
);
$nodes = node_load_multiple(array_keys($node_info));
foreach ($nodes as $node) {
$empty_fields = array();
foreach ($fields[$node->type] as $field_name) {
$items = field_get_items('node', $node, $field_name);
if (!$items) {
$empty_fields[] = $field_name;
}
}
if (!empty($empty_fields)) {
$author = user_load($node->uid);
$group_nodes = array();
if ($space_items = field_get_items('node', $node, 'og_group_ref')) {
foreach ($space_items as $item) {
$group = node_load($item['target_id']);
$group_nodes[] = $group->title;
}
}
fputcsv(
$handle,
array(
$node->nid,
$node->title,
$node->type,
format_date($node->created, 'd/m/Y'),
implode(', ', $group_nodes),
'https://www.humanitarianresponse.info/' . drupal_get_path_alias(
'node/' . $node->nid
),
$author->name,
implode(', ', $empty_fields)
),
';'
);
}
}
$context['results']['count'] += $actual_limit;
$context['finished'] = $context['results']['count'] / $context['sandbox']['total_nodes'];
}
else {
$context['finished'] = 1;
}
fclose($handle);
$context['message'] = t(
'Processed @count of @total nodes.',
array(
'@count' => $context['results']['count'],
'@total' => $context['sandbox']['total_nodes'],
)
);
}
/**
* The batch finish handler.
*/
function {my_module}_mcm_process_batch_finished($success, $results, $operations) {
if ($success) {
drupal_set_message('Batch is complete!');
}
else {
$error_operation = reset($operations);
$message = t('An error occurred while processing %error_operation with arguments: @arguments', array(
'%error_operation' => $error_operation[0],
'@arguments' => print_r($error_operation[1], TRUE)
));
drupal_set_message($message, 'error');
}
drupal_set_message(l('Run again', 'check-mandatory-fields'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment