You are here

function block_attributes_form_submit in Block Attributes 7

Helper function: additional submit callback for block configuration pages.

Save supplied HTML attributes values.

1 string reference to 'block_attributes_form_submit'
_block_attributes_form_alter in ./block_attributes.module
Add the block attributes fields to a block add or configuration form.

File

./block_attributes.module, line 339
Enhanced control over the HTML attributes of any Block.

Code

function block_attributes_form_submit($form, &$form_state) {

  // Form ids of modules with block creation pages also need to be checked.
  if (in_array($form_state['values']['form_id'], array(
    'block_admin_configure',
    'block_add_block_form',
    'menu_block_add_block_form',
  ))) {
    $groups = array(
      BLOCK_ATTRIBUTES_BLOCK,
      BLOCK_ATTRIBUTES_TITLE,
      BLOCK_ATTRIBUTES_CONTENT,
    );
    $block_attributes = array();
    foreach ($groups as $group) {

      // Trim values, filter empty ones and check other ones for plain text.
      $block_attributes[$group] = array_map('check_plain', array_filter(array_map('trim', $form_state['values'][$group])));

      // Convert multiple CSS classes to be stored as an array.
      if (!empty($block_attributes[$group]['class'])) {
        $block_attributes[$group]['class'] = array_filter(explode(' ', $block_attributes[$group]['class']));
      }
    }

    // Filter empty group array values with a call to array_filter.
    // Preparation of the serialized block attributes to be saved.
    $block_attributes = serialize(array_filter($block_attributes));

    // Only save if the block attributes values have changed.
    if ($block_attributes != $form_state['values']['serialized_attributes'] && user_access('administer blocks')) {
      db_update('block')
        ->fields(array(
        'options' => $block_attributes,
      ))
        ->condition('module', $form_state['values']['module'])
        ->condition('delta', $form_state['values']['delta'])
        ->execute();

      // Flush all context module cache to use the updated css_class.
      if (module_exists('context')) {
        cache_clear_all('context', 'cache', TRUE);
      }
    }
  }
}