You are here

function entityreference_rendered_widget_attach_fields in Entityreference Rendered Widget 7

Return attached checkboxes.

Parameters

string $entity_type: String containing the type of entity.

string $parents_path: String parents_path.

array $field: Array contains field.

string $langcode: String language.

array $items: (Optional) Array of all marked items.

array $all_entities: (Optional) Array containing all entities.

Return value

array Array to be added to Element.

1 call to entityreference_rendered_widget_attach_fields()
entityreference_rendered_widget_field_widget_form in ./entityreference_rendered_widget.module
Implements hook_field_widget_form().

File

./entityreference_rendered_widget.module, line 157
Entityreference Rendered Widget module.

Code

function entityreference_rendered_widget_attach_fields($entity_type, $parents_path, array $field, $langcode, $view_mode, array $items = array(), array $all_entities = array(), $label_display = 'before') {
  $type = 'radio';
  if ($field['cardinality'] != 1) {

    // Change type.
    $type = 'checkbox';
  }

  // Define empty array.
  $rows = array();

  // Add any value if radio type.
  if ($type == 'radio') {
    $name = $field['field_name'] . "[{$langcode}][0][target_id]";

    // Any values.
    $rows[0]['target_id'] = array(
      '#type' => $type,
      '#delta' => 0,
      '#return_value' => NULL,
      '#default_value' => entityreference_rendered_widget_checkbox_default_value(0, $items),
      '#title' => t('- Any -'),
      '#name' => $name,
    );
  }
  $delta = 1;
  foreach ($all_entities as $ids) {
    $ids = array_keys($ids);

    // Load entities.
    $entities = entity_load($entity_type, $ids);
    foreach ($entities as $entity_id => $entity) {
      $label = $label_display == 'hidden' ? '' : '<div class="entity-label">' . entity_label($entity_type, $entity) . '</div>';
      $entity_view = entity_view($entity_type, array(
        $entity,
      ), $view_mode);
      $suffix = '<div class="view-mode">';
      $suffix .= drupal_render($entity_view) . '</div>';
      $title = $label_display == 'before' ? $label . $suffix : $suffix . $label;
      $rows[$delta] = array(
        'target_id' => array(
          '#type' => $type,
          '#delta' => $delta,
          '#return_value' => $entity_id,
          '#default_value' => entityreference_rendered_widget_checkbox_default_value($entity_id, $items),
          '#title' => $title,
        ),
      );

      // Set same name if radio button.
      if (isset($name)) {
        $rows[$delta]['target_id']['#name'] = $name;
      }
      ++$delta;
    }
  }
  return $rows;
}