Commit 49d1c220 authored by dragonwize's avatar dragonwize
Browse files

Added #358293 by dragonwize: Block support added.

parent 6a24dfa8
......@@ -9,7 +9,7 @@ Features
* Expand the selection fieldset by default.
* Disable the expand/collapse of the selection fieldset.
* Set selection fieldset title.
* Set default formats for nodes and comments separately.
* Set default formats for nodes, comments, and blocks separately.
* Works with CCK textareas.
* Panels comment support.
* I18n module support.
......
......@@ -3,7 +3,7 @@
/**
* @file
* Default theme implementation to configure format manager filter admin page.
* Default theme implementation to configure Better Formats defaults admin page.
*
* Available variables:
* - $form_submit: Form submit button.
......@@ -20,15 +20,16 @@
// Add table javascript
drupal_add_tabledrag('node-format-defaults', 'order', 'sibling', 'better-formats-role-node-weight');
drupal_add_tabledrag('comment-format-defaults', 'order', 'sibling', 'better-formats-role-comment-weight');
drupal_add_tabledrag('block-format-defaults', 'order', 'sibling', 'better-formats-role-block-weight');
?>
<div class="description"><?php print t('Only formats that a role has permission to use are shown.'); ?></div>
<fieldset>
<legend><strong><?php print t('Node Defaults'); ?></strong></legend>
<legend><strong><?php print t('Node defaults'); ?></strong></legend>
<table id="node-format-defaults">
<thead>
<tr>
<th><?php print t('Role'); ?></th>
<th><?php print t('Default Format'); ?></th>
<th><?php print t('Default format'); ?></th>
<th><?php print t('Weight'); ?></th>
</tr>
</thead>
......@@ -47,12 +48,12 @@
</fieldset>
<fieldset>
<legend><strong><?php print t('Comment Defaults'); ?></strong></legend>
<legend><strong><?php print t('Comment defaults'); ?></strong></legend>
<table id="comment-format-defaults">
<thead>
<tr>
<th><?php print t('Role'); ?></th>
<th><?php print t('Default Format'); ?></th>
<th><?php print t('Default format'); ?></th>
<th><?php print t('Weight'); ?></th>
</tr>
</thead>
......@@ -70,4 +71,31 @@
</table>
</fieldset>
<?php if (isset($block_default_rows)): ?>
<fieldset>
<legend><strong><?php print t('Block defaults'); ?></strong></legend>
<table id="block-format-defaults">
<thead>
<tr>
<th><?php print t('Role'); ?></th>
<th><?php print t('Default format'); ?></th>
<th><?php print t('Weight'); ?></th>
</tr>
</thead>
<tbody>
<?php $row = 0; ?>
<?php foreach ($block_default_rows as $rid => $data): ?>
<tr class="draggable <?php print $row % 2 ? 'odd' : 'even'; ?>">
<td class=""><?php print $data->role; ?></td>
<td><?php print $data->format_select; ?></td>
<td><?php print $data->weight_select; ?></td>
</tr>
<?php $row++; ?>
<?php endforeach; ?>
</tbody>
</table>
<div class="description"><?php print t('Only roles that have "administer blocks" permission are shown.'); ?></div>
</fieldset>
<?php endif; ?>
<?php print $form_submit; ?>
......@@ -74,6 +74,7 @@ function better_formats_install() {
foreach ($roles as $rid => $role) {
db_query($sql, $rid, 'node', 0, 1, 0);
db_query($sql, $rid, 'comment', 0, 1, 0);
db_query($sql, $rid, 'block', 0, 1, 0);
}
// Set default perms to be like core defaults.
......
......@@ -105,6 +105,11 @@ function better_formats_form_alter(&$form, $form_state, $form_id) {
case 'node-form':
better_formats_set_node_format($form);
break;
case 'block-add-block-form':
case 'block-admin-configure':
better_formats_set_block_format($form);
break;
}
// Alter role add/delete and node type forms.
......@@ -242,6 +247,7 @@ function better_formats_new_role($form, &$form_state) {
VALUES (%d, '%s', %d, %d, %d)";
db_query($sql, $rid, 'node', 0, 1, 25);
db_query($sql, $rid, 'comment', 0, 1, 25);
db_query($sql, $rid, 'block', 0, 1, 25);
}
/**
......@@ -370,6 +376,30 @@ function better_formats_set_comment_format(&$form) {
}
}
/**
* Processes formats for core block form.
*
* @see better_formats_form_alter()
*/
function better_formats_set_block_format(&$form) {
if (isset($form['block_settings']['body_field']['format'])) {
// Get BF default format.
$default = better_formats_get_default_format('block');
if (empty($form['delta']['#value'])) {
// Set format to default for new entries.
$format = $default;
}
else {
// Get existing format for block.
$format = better_formats_get_current_format($form['block_settings']['body_field']['format']);
}
// Overwrite the filter form with our own.
$form['block_settings']['body_field']['format'] = better_formats_filter_form($format, $default);
}
}
/**
* Returns the format for an existing node or comment.
*
......@@ -402,7 +432,7 @@ function better_formats_get_current_format($form) {
* Returns the default format for an new node or comment.
*
* @param $mode
* 'node' or 'comment'. Describes the top level type of default.
* 'node', 'comment', or 'block'. Describes the top level type of default.
* @return
* Format ID.
*
......@@ -431,7 +461,7 @@ function better_formats_get_default_format($mode, $node_type = '') {
$roles = implode(',', array_keys($user->roles));
// Prepare types for SQL.
if ($per_node_type && $node_type) {
if ($mode !== 'block' && $per_node_type && $node_type) {
$types .= "','" . $mode;
}
......@@ -485,7 +515,8 @@ function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_for
else if (isset($formats[$default_format])) {
// Use currently set BF default as a fallback.
$default = $default_format;
} else if (!empty($formats)) {
}
else if (!empty($formats)) {
// Current and default format are not allowed, so use first allowed format.
reset($formats);
$default = key($formats);
......
......@@ -23,7 +23,8 @@ function better_formats_defaults_admin_form() {
$nform = better_formats_get_role_default_fields('node');
$cform = better_formats_get_role_default_fields('comment');
$form = array_merge($form, $nform, $cform);
$bform = better_formats_get_role_default_fields('block');
$form = array_merge($form, $nform, $cform, $bform);
$form['submit'] = array(
'#type' => 'submit',
......@@ -45,10 +46,10 @@ function better_formats_defaults_admin_form_validate($form, &$form_state) {
$roles[$fid] = explode(',', $format->roles);
}
// Get roles that have administer filters permission.
$admin_roles = better_formats_get_filter_admin_roles();
$admin_roles = better_formats_get_roles_by_perm('administer filters');
foreach ($form_state['values'] as $key => $values) {
if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0) {
if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0 || strpos($key, 'block-') === 0) {
list($type, $rid) = explode('-', $key);
if (in_array($rid, $admin_roles)) {
// Role has the 'administer filters' permission so it can use all formats.
......@@ -75,7 +76,7 @@ function better_formats_defaults_admin_form_submit($form, &$form_state) {
WHERE rid=%d AND type='%s'";
foreach ($form_state['values'] as $key => $values) {
if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0) {
if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0 || strpos($key, 'block-') === 0) {
list($type, $rid) = explode('-', $key);
db_query($sql, $values['format'], $values['weight'], $rid, $type);
}
......@@ -88,7 +89,7 @@ function better_formats_defaults_admin_form_submit($form, &$form_state) {
* Builds FAPI form elements for the default format selection.
*
* @param $mode
* 'node' or 'comment'. Top most level type for requested default.
* 'node', 'comment', or 'block'. Top most level type for requested default.
* @param $node_type
* Type of node this request is for.
* @return
......@@ -99,6 +100,7 @@ function better_formats_get_role_default_fields($mode, $node_type = '') {
$format_options = better_formats_get_formats_per_role();
$type = $types = $mode;
$per_node_type = variable_get('better_formats_per_node_type', FALSE);
$block_admins = better_formats_get_roles_by_perm('administer blocks');
if ($per_node_type && $node_type) {
$type = $mode . '/' . $node_type;
......@@ -117,7 +119,7 @@ function better_formats_get_role_default_fields($mode, $node_type = '') {
$roles_set = array();
while ($role = db_fetch_object($result)) {
if (in_array($role->rid, $roles_set)) {
if (in_array($role->rid, $roles_set) || ($mode === 'block' && !in_array($role->rid, $block_admins))) {
continue;
}
......@@ -161,7 +163,7 @@ function better_formats_get_formats_per_role() {
$roles = user_roles();
// Get roles that have administer filters permission.
$admin_roles = better_formats_get_filter_admin_roles();
$admin_roles = better_formats_get_roles_by_perm('administer filters');
$site_default_format = filter_resolve_format(FILTER_FORMAT_DEFAULT);
......@@ -179,25 +181,25 @@ function better_formats_get_formats_per_role() {
}
/**
* Get a list of roles that have the admin filter perm.
* Get a list of roles that have a permission.
*
* @return
* An numeric indexed key of role IDs.
*/
function better_formats_get_filter_admin_roles() {
function better_formats_get_roles_by_perm($perm) {
static $roles;
if (!$roles) {
if (!$roles[$perm]) {
$sql = "SELECT rid
FROM {permission}
WHERE perm LIKE '%administer filters%'
WHERE perm LIKE '%$perm%'
ORDER BY rid";
$result = db_query($sql);
$roles = array();
while ($row = db_fetch_object($result)) {
$roles[] = $row->rid;
$roles[$perm][] = $row->rid;
}
}
return $roles;
return $roles[$perm];
}
/**
......@@ -207,11 +209,12 @@ function better_formats_get_filter_admin_roles() {
* The $variables array contains the following arguments:
* - $form
*/
function template_preprocess_better_formats_defaults_admin_form(&$variables) {
foreach (element_children($variables['form']) as $key) {
$form_row = &$variables['form'][$key];
function template_preprocess_better_formats_defaults_admin_form(&$vars) {
foreach (element_children($vars['form']) as $key) {
$form_row = &$vars['form'][$key];
$type = strpos($key, 'node-') === 0 ? 'node' : 'comment';
//$type = strpos($key, 'node-') === 0 ? 'node' : 'comment';
$type = substr($key, 0, strpos($key, '-'));
if (isset($form_row['role'])) {
// Set special classes needed for table drag and drop.
......@@ -222,9 +225,9 @@ function template_preprocess_better_formats_defaults_admin_form(&$variables) {
$row->format_select = drupal_render($form_row['format']);
$row->weight_select = drupal_render($form_row['weight']);
$variables[$type . '_default_rows'][$key] = $row;
$vars[$type . '_default_rows'][$key] = $row;
}
}
$variables['form_submit'] = drupal_render($variables['form']);
$vars['form_submit'] = drupal_render($vars['form']);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment