You are here

function mee_field_widget_form_alter in Scald: Media Management made easy 7

Implements hook_field_widget_form_alter().

File

modules/fields/mee/mee.module, line 447
Defines a special textarea, with drag and drop media driven by Scald and dnd.module.

Code

function mee_field_widget_form_alter(&$element, &$form_state, $context) {
  $enables = _mee_field_instance_enabled($context['instance']);

  // In any case, convert SAS into rendered for format textarea.
  if (in_array($context['field']['type'], mee_field_types()) && isset($element['#default_value'])) {
    if (mee_store_format() == 'sas') {
      $element['#default_value'] = scald_sas_to_rendered($element['#default_value'], $context['instance']['settings']['context'], FALSE, dnd_scald_wysiwyg_context_slugs());
    }
  }

  // Activate DnD Library for this element if enabled.
  if (!empty($enables['dnd'])) {
    $settings = $context['instance']['settings'];
    $context_default = isset($settings['context_default']) ? $settings['context_default'] : variable_get('dnd_context_default', 'sdl_editor_representation');
    $element['#attributes']['data-dnd-context'][] = $context_default;
    $element['#attached']['library'][] = array(
      'dnd',
      'library',
    );
    if (isset($element['summary'])) {
      $element['summary']['#attributes']['data-dnd-context'][] = $context_default;
    }
  }

  // Add our custom form element into MEE enabled textarea only.
  if (empty($enables['mee'])) {
    return;
  }
  $element['mee'] = array(
    '#prefix' => '<div class="mee-wrap-editor-library">',
    '#suffix' => '</div>',
    '#attached' => array(
      'library' => array(
        array(
          'mee',
          'library',
        ),
      ),
    ),
    '#element_validate' => array(
      'mee_field_text_validate',
    ),
    '#weight' => 0.5,
    'resource_manager' => array(
      '#theme' => 'mee_resource_manager',
    ),
  );
  $resource_manager = array();

  // 'input' is used instead of 'values' because we need extra items inserted
  // using JavaScript on the client side.
  if (isset($form_state['input'][$context['field']['field_name']][$context['langcode']])) {
    $resource_manager = $form_state['input'][$context['field']['field_name']][$context['langcode']][$context['delta']]['mee']['resource_manager'];
  }
  elseif (isset($element['#entity'])) {
    $item = array();
    _mee_load_resources($element['#entity_type'], $element['#entity'], $context['field'], $context['delta'], $item);
    $resource_manager = $item['mee']['resource_manager'];
  }
  foreach ($resource_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']['resource_manager'][$sid] = array(
      'title' => array(
        '#markup' => $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']['resource_manager'][0] = array(
    'title' => array(
      '#markup' => t('< Primary / Secondary >'),
    ),
    'required' => array(
      '#markup' => '-',
    ),
    'weight' => array(
      '#type' => 'weight',
      '#prefix' => '<div class="mee-rm-separator">',
      '#suffix' => '</div>',
    ),
    '#weight' => isset($resource_manager[0]['weight']) ? $resource_manager[0]['weight'] : 0,
  );
}