You are here

protected function MaestroWebformTask::_getAttachedNodeOptions in Maestro 8.2

Same name and namespace in other branches
  1. 3.x modules/maestro_webform/src/Plugin/EngineTasks/MaestroWebformTask.php \Drupal\maestro_webform\Plugin\EngineTasks\MaestroWebformTask::_getAttachedNodeOptions()

Internal used method to get attached node options.

2 calls to MaestroWebformTask::_getAttachedNodeOptions()
MaestroWebformTask::fetchNodesAttached in modules/maestro_webform/src/Plugin/EngineTasks/MaestroWebformTask.php
Ajax callback to validate the Webform selection. Generate a list of nodes it's attached to, if any.
MaestroWebformTask::getTaskEditForm in modules/maestro_webform/src/Plugin/EngineTasks/MaestroWebformTask.php
Method to allow a task to add their own fields to the task edit form.

File

modules/maestro_webform/src/Plugin/EngineTasks/MaestroWebformTask.php, line 468

Class

MaestroWebformTask
Maestro Webform Task Plugin.

Namespace

Drupal\maestro_webform\Plugin\EngineTasks

Code

protected function _getAttachedNodeOptions($webform_machine_name) {
  $options = [];
  if ($webform_machine_name != '') {
    $field_configs = \Drupal::entityTypeManager()
      ->getStorage('field_config')
      ->loadByProperties([
      'entity_type' => 'node',
    ]);
    $node_types = NodeType::loadMultiple();
    $ntypes = [];
    $fnames = [];
    foreach ($field_configs as $field_config) {
      if ($field_config
        ->get('field_type') === 'webform') {
        $bundle = $field_config
          ->get('bundle');
        $ntypes[$bundle] = $node_types[$bundle];
        $field_name = $field_config
          ->get('field_name');
        $fnames[$field_name] = $field_name;
      }
    }
    if (count($fnames) > 0) {
      $query = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->getQuery();
      $or = $query
        ->orConditionGroup();
      foreach ($fnames as $field_name) {
        $or
          ->condition($field_name . '.target_id', $webform_machine_name);
      }
      $query
        ->condition($or);
      $result = $query
        ->execute();

      // The result are now node IDs we can use to add to the options.
      $entities = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->loadMultiple($result);
      foreach ($entities as $id => $node) {
        $options['node/' . $id] = $node
          ->label();
      }
    }
  }
  if (count($options)) {
    $options = array_merge([
      'none' => $this
        ->t('Not Selected'),
    ], $options);
  }
  else {
    $options['none'] = $this
      ->t('No nodes attach this webform');
  }
  return $options;
}