You are here

function mee_textarea_process in Scald: Media Management made easy 6

Process an individual element.

Build the form element. When creating a form using FAPI #process, note that $element['#value'] is already set.

The $fields array is in $form['#field_info'][$element['#field_name']].

1 string reference to 'mee_textarea_process'
mee_elements in mee/mee.module
Implementation of FAPI hook_elements().

File

mee/mee.module, line 446
Defines a special textarea, with drag and drop media driven by Scald and dnd.module when rich text editing is enabled on the textarea via the WYSIWYG API.

Code

function mee_textarea_process($element, $edit, $form_state, $form) {
  drupal_add_css(drupal_get_path('module', 'mee') . '/css/mee.css');
  drupal_add_js(drupal_get_path('module', 'mee') . '/mee.js');
  $element['mee'] = array(
    '#type' => 'markup',
    '#prefix' => '<div class="mee-wrap-editor-library">',
    '#suffix' => '</div>',
  );
  $field = $form['#field_info'][$element['#field_name']];
  $field_key = $element['#columns'][0];
  $element['mee']['ressource_manager'] = array(
    '#type' => 'markup',
    '#weight' => 0.5,
    '#theme' => 'mee_ressource_manager',
  );
  if (!isset($element['#value']['mee']['ressource_manager'])) {
    $element['#value']['mee']['ressource_manager'] = array();

    // Restore/Generate the associated ressources in a proper order
    $query = "SELECT * FROM {mee_ressources} WHERE content_nid=%d AND field='%s' ORDER BY weight ASC";
    $result = db_query($query, $form['nid']['#value'], $element['#field_name']);
    while ($item = db_fetch_object($result)) {
      $element['#value']['mee']['ressource_manager'][$item->atom_sid] = (array) $item;
    }
    $element['#value']['mee']['ressource_manager'][0] = array(
      'weight' => 0,
    );
  }
  foreach ($element['#value']['mee']['ressource_manager'] as $sid => $item) {
    $atom = scald_fetch($sid);
    if (!is_object($atom)) {
      continue;
    }

    // Render the atom to get sanitized values.
    $title = scald_render($atom, 'title');
    $element['mee']['ressource_manager'][$sid] = array(
      'title' => array(
        '#type' => 'markup',
        '#value' => $title,
      ),
      'required' => array(
        '#type' => 'select',
        '#options' => array(
          t('Optional'),
          t('Required'),
        ),
        '#default_value' => $item['required'],
      ),
      'weight' => array(
        '#type' => 'weight',
        '#default_value' => $item['weight'],
      ),
      '#weight' => $item['weight'],
    );
  }

  // And now we add the separator
  $element['mee']['ressource_manager'][0] = array(
    'title' => array(
      '#type' => 'markup',
      '#value' => t('< Primary / Secondary >'),
    ),
    'required' => array(
      '#type' => 'markup',
      '#value' => '-',
    ),
    'weight' => array(
      '#type' => 'weight',
      '#prefix' => '<div class="mee-rm-separator">',
      '#suffix' => '</div>',
    ),
    '#weight' => $element['#value']['mee']['ressource_manager'][0]['weight'],
  );
  if (isset($element['#value'][$field_key])) {
    $element['#value']['mee'][$field_key] = $element['#value'][$field_key];
  }
  $element['mee'][$field_key] = array(
    '#type' => 'textarea',
    '#default_value' => isset($element['#value']['mee'][$field_key]) ? $element['#value']['mee'][$field_key] : NULL,
    '#rows' => !empty($field['widget']['rows']) ? $field['widget']['rows'] : 60,
    '#weight' => 0,
    // The following values were set by the content module and need
    // to be passed down to the nested element.
    '#title' => $element['#title'],
    '#description' => $element['#description'],
    '#required' => $element['#required'],
    '#field_name' => $element['#field_name'],
    '#type_name' => $element['#type_name'],
    '#delta' => $element['#delta'],
    '#columns' => $element['#columns'],
    '#dnd-enabled' => TRUE,
    '#dnd-settings' => array(
      'drop_selector' => '#' . $element['#id'] . ' .drop',
    ),
  );
  if (!empty($field['mee_processing'])) {
    $filter_key = count($element['#columns']) == 2 ? $element['#columns'][1] : 'format';
    $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
    $parents = array_merge($element['#parents'], array(
      $filter_key,
    ));
    $element['mee'][$filter_key] = filter_form($format, 1, $parents);
    $element['mee'][$filter_key]['#prefix'] = '<div class="mee-filter-form">';
    $element['mee'][$filter_key]['#suffix'] = '</div>';
  }

  // Used so that hook_field('validate') knows where to flag an error.
  $element['_error_element'] = array(
    '#type' => 'value',
    '#value' => implode('][', array_merge($element['#parents'], array(
      $field_key,
    ))),
  );
  $short = isset($element['#value']['short']) ? $element['#value']['short'] : $element['#value']['mee']['short'];
  $element['mee']['short'] = array(
    '#type' => 'textarea',
    '#title' => t('Short content'),
    '#rows' => 5,
    '#weight' => -100,
    '#default_value' => $short,
  );
  return $element;
}