You are here

public function ProfileAddLocalTask::getDerivativeDefinitions in Profile 2 8

Gets the definition of all derivatives of a base plugin.

Parameters

array $base_plugin_definition: The definition array of the base plugin.

Return value

array An array of full derivative definitions keyed on derivative id.

Overrides DeriverBase::getDerivativeDefinitions

See also

getDerivativeDefinition()

File

src/Plugin/Derivative/ProfileAddLocalTask.php, line 71
Contains \Drupal\profile\Plugin\Derivative\ProfileAddLocalTask.

Class

ProfileAddLocalTask
Provides dynamic routes to add profiles.

Namespace

Drupal\profile\Plugin\Derivative

Code

public function getDerivativeDefinitions($base_plugin_definition) {
  $this->derivatives = array();
  $user = \Drupal::request()->attributes
    ->get('user');
  $route_name = \Drupal::routeMatch()
    ->getRouteName();
  if ($user instanceof UserInterface && $route_name == 'entity.user.edit_form') {
    PhpStorageFactory::get('service_container')
      ->deleteAll();
    PhpStorageFactory::get('twig')
      ->deleteAll();
    drupal_flush_all_caches();
    $configs = array();
    foreach ($this->config as $config) {
      $instances = array_filter($this->entityManager
        ->getFieldDefinitions('profile', $config
        ->get('id')), function ($field_definition) {
        return $field_definition instanceof FieldConfigInterface;
      });
      $display = FALSE;

      // No fields yet.
      if (!count($instances)) {
        continue;
      }
      else {

        // Expose profile types that users may create - either they have 0 of non-multiple or multiple.
        if ($config
          ->get('multiple') === FALSE) {
          $profiles = $this->entityManager
            ->getStorage('profile')
            ->loadByProperties(array(
            'uid' => $user
              ->id(),
            'type' => $config
              ->get('id'),
          ));

          // Single profile, none yet.
          if (!count($profiles)) {
            $display = TRUE;
            $configs[] = $config;
          }
        }
        else {

          // Multiple profiles allowed.
          $display = TRUE;
          $configs[] = $config;
        }
        if ($display) {
          $id = $config
            ->get('id') . '-' . $user
            ->id();
          $this->derivatives[$id] = $base_plugin_definition;
          $this->derivatives[$id]['title'] = \Drupal::translation()
            ->translate('Add @type profile', array(
            '@type' => Unicode::strtolower($config
              ->get('label')),
          ));
          $this->derivatives[$id]['route_parameters'] = array(
            'user' => $user
              ->id(),
            'type' => $config
              ->get('id'),
          );
        }
      }
    }
  }
  return $this->derivatives;
}