You are here

function draggableviews_handler_field_draggable::views_form in DraggableViews 7.2

The form which replaces the placeholder from render().

File

views/draggableviews_handler_field_draggable.inc, line 150
Views field handler. Contains all relevant Draggableviews options and related logic. Implements the Views Form API.

Class

draggableviews_handler_field_draggable
@file Views field handler. Contains all relevant Draggableviews options and related logic. Implements the Views Form API.

Code

function views_form(&$form, &$form_state) {

  // The view is empty, abort.
  if (empty($this->view->result)) {
    return;
  }

  // If this view is a summary, do not add a form.
  if ($this->field_alias == 'unknown') {
    return;
  }
  $form[$this->options['id']] = array(
    '#tree' => TRUE,
  );
  $range = count($this->view->result);

  // At this point, the query has already been run, so we can access the results
  // in order to get the base key value (for example, nid for nodes).
  foreach ($this->view->result as $row_index => $row) {
    $entity_id = $this
      ->get_value($row);
    $form[$this->options['id']][$row_index] = array(
      '#tree' => TRUE,
    );
    $handler_object = draggableviews_get_handler_class($this->options['draggableviews']['handler']);

    // Weight field selectbox.
    $form[$this->options['id']][$row_index]['weight'] = array(
      '#type' => 'select',
      '#options' => range(-$range, $range),
      '#attributes' => array(
        'class' => array(
          'draggableviews-weight',
        ),
      ),
      '#default_value' => $handler_object
        ->get($this, $row_index),
    );

    // Item to keep id of the entity.
    $form[$this->options['id']][$row_index]['id'] = array(
      '#type' => 'hidden',
      '#value' => $this->view->result[$row_index]->{$this->field_alias},
      '#attributes' => array(
        'class' => array(
          'draggableviews-id',
        ),
      ),
    );

    // Add parent and depth field.
    if (!empty($this->options['draggableviews']['hierarchy_handler'])) {
      $hierarchy_handler_object = draggableviews_get_handler_class($this->options['draggableviews']['hierarchy_handler'], 'hierarchy_handler');
      $form[$this->options['id']][$row_index]['parent'] = array(
        '#type' => 'hidden',
        '#default_value' => $hierarchy_handler_object
          ->get($this, $row_index),
        '#attributes' => array(
          'class' => array(
            'draggableviews-parent',
          ),
        ),
      );
      $form[$this->options['id']][$row_index]['depth'] = array(
        '#type' => 'hidden',
        '#default_value' => $hierarchy_handler_object
          ->get_depth($this, $row_index),
        '#attributes' => array(
          'class' => array(
            'draggableviews-depth',
          ),
        ),
      );
    }
  }
}