You are here

class ProfileAddLocalTask in Profile 2 8

Provides dynamic routes to add profiles.

Hierarchy

Expanded class hierarchy of ProfileAddLocalTask

1 string reference to 'ProfileAddLocalTask'
profile.links.task.yml in ./profile.links.task.yml
profile.links.task.yml

File

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

Namespace

Drupal\profile\Plugin\Derivative
View source
class ProfileAddLocalTask extends DeriverBase implements ContainerDeriverInterface {

  /**
   * Stores the profile type config objects.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * The entity manager service
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Constructs a new ThemeLocalTask.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   */
  public function __construct($base_plugin_definition, ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager) {
    $this->config = $config_factory
      ->loadMultiple($config_factory
      ->listAll('profile.type'));
    $this->entityManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_definition) {
    return new static($base_plugin_definition, $container
      ->get('config.factory'), $container
      ->get('entity.manager'));
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
ProfileAddLocalTask::$config protected property Stores the profile type config objects.
ProfileAddLocalTask::$entityManager protected property The entity manager service
ProfileAddLocalTask::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
ProfileAddLocalTask::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
ProfileAddLocalTask::__construct public function Constructs a new ThemeLocalTask.