Enchanced administrative interface
Alan D. - March 19, 2009 - 11:13
| Project: | Easy Image Insert |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | minor |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
Nice module. It is a very good concept, I was considering doing a similar thing before I found yours.
The project we are working on has a large number of content types, 25, so enabling this on a per-content type is a bit of a drag. The following code fragment adds a settings page that lists all content types and provides a checkbox for each type to enable / disable.
<?php
/**
* Implementation of hook_menu().
*/
function easy_image_insert_menu() {
$items = array();
$items['admin/settings/easy_image_insert'] = array(
'title' => 'Easy image insert',
'description' => 'Adjust easy image insert settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('easy_image_insert_settings'),
'access arguments' => array('administer site configuration'),
);
return $items;
}
/**
* Custom settings form for all content types.
*
* @return $form
*/
function easy_image_insert_settings() {
$form = array();
$form['eii_info'] = array(
'#title' => t('Enabled content types'),
'#type' => 'markup',
'#value' => t('Select the content types that you would like the Easy Image Insert module enabled on.'),
);
$types = node_get_types('names');
foreach ($types as $type => $name) {
$form['eii_'. $type .'_active'] = array(
'#type' => 'checkbox',
'#title' => t('Enable on %name', array('%name' => $name)),
'#default_value' => variable_get('eii_'. $type .'_active', 0)
);
}
return system_settings_form($form);
}
?>
#1
Rather than creating multiple issues, this is a related bug that occurs when you are creating a new content type.
When setting the setting on a new content type, the following variable is created: "eii__active" instead of the correct type of "eii_[NEW TYPE MACHINE NAME]_active". Using the user submitted value resolves this.
Also, when a content type is deleted, it would be nice to remove the corresponding variable. The hook_node_type is used to do this.
Thirdly, instead of having the lone checkbox in the fieldset, it would be nice to add this to the Identification fieldset, just under the body field.
Lastly, using module_form_alter_submit makes for confusing reading. IMHO, easy_image_insert_form_node_type_form_alter_submit would be more appropriate, albeit very wordy! Maybe just eii_formid_submit?
The following code fragment should help address all of these issues / requests.
<?php
/**
* Implementation of hook_node_type().
*
* This cleans up the variables table as types are deleted.
*/
function easy_image_insert_node_type($op, $info) {
switch ($op){
case 'delete':
variable_del('eii_'. $info->type .'_active');
break;
}
}
/**
* Implementation of hook_[form_id]_alter().
*
* Not really sure if this is classed as a hook or a callback,
* or something else. Nice to know that it exists, :)
*/
function easy_image_insert_form_node_type_form_alter(&$form, &$form_state) {
$form['identity']['eii_active'] = array(
'#type' => 'checkbox',
'#title' => t('Activate Easy Image Insert Options'),
'#default_value' => variable_get('eii_' . $form['#node_type']->type . '_active', 0)
);
$form['#submit'][] = 'easy_image_insert_form_node_type_form_alter_submit';
}
/**
* Custom submit handler.
*
* For existing content types, the node_type object can be used,
* but if the form is for a new type, this value will be an empty
* string. Using the user submitted machine name overcomes this.
*/
function easy_image_insert_form_node_type_form_alter_submit($form, $form_state) {
$type = empty($form['#node_type']->type) ? $form['identity']['type']['#value'] : $form['#node_type']->type;
variable_set('eii_'. $type . '_active', $form_state['values']['eii_active']);
}
?>
#2
@ Alan D. See a simpler solution to your issue at #407728: Duplicate variable entries. I do like your idea to delete the variable when the node type is removed though.
#3
Thanks Daniel, I forgot about the magic in the content_type form.
So the above changes, after applying #407728: Duplicate variable entries patch, are:
1) First two functions would adds the ability to configure all content types in one hit
2) The third addresses automatic deletion when a content type is deleted.
3) The last one moves the checkbox to the identity fieldset.
<?php
/**
* Implementation of hook_menu().
*/
function easy_image_insert_menu() {
$items = array();
$items['admin/settings/easy_image_insert'] = array(
'title' => 'Easy image insert',
'description' => 'Adjust easy image insert settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('easy_image_insert_settings'),
'access arguments' => array('administer site configuration'),
);
return $items;
}
/**
* Custom settings form for all content types.
*
* @return $form
*/
function easy_image_insert_settings() {
$form = array();
$form['eii_info'] = array(
'#title' => t('Enabled content types'),
'#type' => 'markup',
'#value' => t('Select the content types that you would like the Easy Image Insert module enabled on.'),
);
$types = node_get_types('names');
foreach ($types as $type => $name) {
$form['eii_active_'. $type] = array(
'#type' => 'checkbox',
'#title' => t('Enable on %name', array('%name' => $name)),
'#default_value' => variable_get('eii_active_'. $type, 0)
);
}
return system_settings_form($form);
}
/**
* Implementation of hook_node_type().
*
* This cleans up the variables table as types are deleted.
*/
function easy_image_insert_node_type($op, $info) {
switch ($op){
case 'delete':
variable_del('eii_active_'. $info->type);
break;
}
}
/**
* Implementation of hook_[form_id]_alter().
*/
function easy_image_insert_form_node_type_form_alter(&$form, &$form_state) {
$form['identity']['eii_active'] = array(
'#type' => 'checkbox',
'#title' => t('Activate Easy Image Insert Options'),
'#default_value' => variable_get('eii_active_' . $form['#node_type']->type, 0)
);
}
?>
#4
I've created a separate issue regarding the variable deletion, see #408550: Delete variables on node type deletion and module uninstallation.