View source
<?php
function dakwak_perm() {
return array(
'administer nodes',
);
}
function nodeclass_init() {
$settings = variable_get('nodeclass_types', array());
}
function node_class_menu() {
$items['admin/settings/nodeclass'] = array(
'title' => t('Node Class'),
'description' => t('Choose nodes'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'nodeclass_admin_form',
),
'access arguments' => array(
'administer nodes',
),
);
return $items;
}
function nodeclass_admin_form() {
$settings = variable_get('nodeclass_types', array());
$form = array();
$form['types'] = array(
'#type' => 'checkboxes',
'#title' => t('Content types'),
'#options' => array_map('check_plain', node_get_types('names')),
'#description' => t('Select content types to apply classes ability to'),
'#default_value' => $settings['nodeclass_types'],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function nodeclass_admin_form_submit($form_id, $form_values) {
$settings = array(
'nodeclass_types' => $form_values['values']['types'],
);
variable_set('nodeclass_types', $settings);
drupal_set_message(t('Configurations saved'));
}
function node_class_attributes($node) {
$ret = db_fetch_object(db_query('SELECT css_class FROM {node_class} WHERE module = "%s" AND id = "%s"', $node->module, $node->id));
if ($ret !== FALSE) {
return $ret;
}
$undef = (object) NULL;
$undef->css_class = '';
return $undef;
}
function node_class_form_alter(&$form, &$form_state, $form_id) {
$settings = variable_get('nodeclass_types', array());
if (isset($form['#node']) && $form_id == $form['#node']->type . '_node_form') {
$form['node_class'] = array(
'#type' => 'fieldset',
'#title' => t('Node Class settings'),
'#collapsible' => TRUE,
'#weight' => -1,
);
$form['node_class']['css_class'] = array(
'#type' => 'textfield',
'#title' => t('CSS class(es)'),
'#default_value' => $attributes->css_class,
'#description' => t('Separate classes with a space. IMPORTANT: You must add <?php print node_class($block); ?> to your theme\'s node.tpl.php file to make the classes appear.'),
);
$form['#submit'][] = 'node_class_form_submit';
}
}
function node_class_form_submit($form, &$form_state) {
if ($form_state['values']['form_id'] == $form['#node']->type . '_node_form') {
if (isset($form_state['values']['css_class']) && user_access('administer nodes')) {
$module = $form_state['values']['module'];
$id = $form_state['values']['id'];
$class = $form_state['values']['css_class'];
db_query('DELETE FROM {node_class} WHERE module = "%s" AND id = "%s"', $module, $id);
if (!empty($class)) {
db_query('INSERT INTO {node_class} (module, id, css_class) VALUES ("%s", "%s", "%s")', $module, $id, $class);
}
}
}
}
function node_class($node) {
$attributes = node_class_attributes($node);
return check_plain($attributes->css_class);
}