You are here

protected function EntityconnectWidgetProcessor::attachAddButton in Entity connect 8.2

Attach the Add button.

Parameters

array $element: The widget container element.

string[] $entityconnect_classes: Button CSS definition array:

  • 'extra_class': extra css class string
  • 'parents_class': parents class string.

string $key: Default is 'all' (optional).

1 call to EntityconnectWidgetProcessor::attachAddButton()
EntityconnectWidgetProcessor::attachButtons in src/EntityconnectWidgetProcessor.php
Attach the entity connect buttons to a single widget element.

File

src/EntityconnectWidgetProcessor.php, line 202

Class

EntityconnectWidgetProcessor
A reference field widget processing class for entityconnect module.

Namespace

Drupal\entityconnect

Code

protected function attachAddButton(array &$element, array $entityconnect_classes, $key = 'all') {

  // Button values are opposite; 0=On, 1=Off.
  $addbuttonallowed = empty($this->entityconnectSettings['buttons']['button_add']);
  $addIcon = $this->entityconnectSettings['icons']['icon_add'];

  // Get the subset of target bundles the user has permission to create.
  $acceptableTypes = [];
  if (!$this->acceptableTypes) {

    // @FIXME: The acceptable types is ALL so check the access for all.
    if (\Drupal::entityTypeManager()
      ->getAccessControlHandler($this->entityType)
      ->createAccess($this->entityType)) {
      $acceptableTypes[] = $this->entityType;
    }
  }
  else {
    foreach ($this->acceptableTypes as $bundle) {
      if (\Drupal::entityTypeManager()
        ->getAccessControlHandler($this->entityType)
        ->createAccess($bundle)) {
        $acceptableTypes[] = $bundle;
      }
    }
  }

  // Now we need to make sure the user should see this button.
  if (\Drupal::currentUser()
    ->hasPermission('entityconnect add button') && $addbuttonallowed && $acceptableTypes) {
    $classes = [];

    // Determine how the button should be displayed.
    if (isset($addIcon)) {
      if ($addIcon == '0') {
        $classes = $entityconnect_classes['extra_class'] . ' add-icon';
      }
      elseif ($addIcon == '1') {
        $classes = $entityconnect_classes['extra_class'] . ' add-icon add-text';
      }
      else {
        $classes = $entityconnect_classes['extra_class'];
      }
    }

    // Build the button name.
    $button_name = "add_entityconnect__{$this->fieldDefinition->getName()}_{$key}_{$entityconnect_classes['parents_class']}";

    // Build the button element.
    $element[$button_name] = [
      '#type' => 'entityconnect_submit',
      '#value' => $this
        ->t('New content'),
      '#name' => $button_name,
      '#prefix' => "<div class = 'entityconnect-add {$classes}'>",
      '#suffix' => '</div>',
      '#key' => $key,
      '#field' => $this->fieldDefinition
        ->getName(),
      '#entity_type_target' => $this->entityType,
      '#acceptable_types' => $acceptableTypes,
      '#add_child' => TRUE,
      '#weight' => 1,
    ];

    // Button should be at same form level as widget,
    // or text box if multivalue autocomplete field.
    $parents = $this->widget['#parents'];
    if (is_numeric($key)) {
      $parents[] = $key;
    }
    $element[$button_name]['#parents'] = array_merge($parents, [
      $button_name,
    ]);
  }
}