You are here

class ConditionalFieldController in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x src/Controller/ConditionalFieldController.php \Drupal\conditional_fields\Controller\ConditionalFieldController

Returns responses for conditional_fields module routes.

Hierarchy

Expanded class hierarchy of ConditionalFieldController

1 file declares its use of ConditionalFieldController
ConditionalFieldControllerTest.php in tests/src/Unit/ConditionalFieldControllerTest.php
1 string reference to 'ConditionalFieldController'
conditional_fields.services.yml in ./conditional_fields.services.yml
conditional_fields.services.yml
1 service uses ConditionalFieldController
conditional_fields.controller in ./conditional_fields.services.yml
Drupal\conditional_fields\Controller\ConditionalFieldController

File

src/Controller/ConditionalFieldController.php, line 19

Namespace

Drupal\conditional_fields\Controller
View source
class ConditionalFieldController extends ControllerBase {
  protected $entityTypeManager;

  /**
   * Form Builder.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface|EntityFormBuilder
   */
  protected $formBuilder;

  /**
   * Entity type bundle info.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * Entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * ConditionalFieldController constructor.
   *
   * @param EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   * @param FormBuilderInterface $formBuilder
   *   Form builder.
   * @param EntityTypeBundleInfoInterface $entityTypeBundleInfo
   *   Entity type bundle info.
   * @param EntityFieldManagerInterface $entityFieldManager
   *   Entity field manager.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, FormBuilderInterface $formBuilder, EntityTypeBundleInfoInterface $entityTypeBundleInfo, EntityFieldManagerInterface $entityFieldManager) {
    $this->entityTypeManager = $entityTypeManager;
    $this->formBuilder = $formBuilder;
    $this->entityTypeBundleInfo = $entityTypeBundleInfo;
    $this->entityFieldManager = $entityFieldManager;
  }

  /**
   * Show entity types.
   *
   * @return array
   *   Array of page elements to render.
   */
  public function entityTypeList() {
    $output = [
      '#theme' => 'admin_block_content',
      '#content' => [],
    ];
    foreach ($this
      ->getEntityTypes() as $key => $entityType) {
      $output['#content'][] = [
        'url' => Url::fromRoute('conditional_fields.bundle_list', [
          'entity_type' => $key,
        ]),
        'title' => $entityType
          ->getLabel(),
      ];
    }
    return $output;
  }

  /**
   * Title for fields form.
   *
   * @param string $entity_type
   *   Entity type.
   * @param string $bundle
   *   Entity bundle.
   *
   * @return string
   *   Page title.
   */
  public function formTitle($entity_type, $bundle) {
    $bundles = $this->entityTypeBundleInfo
      ->getBundleInfo($entity_type);
    if (!isset($bundles[$bundle]['label'])) {
      return '';
    }
    return $bundles[$bundle]['label'];
  }

  /**
   * Title for field settings form.
   *
   * @param string $entity_type
   *   Entity type.
   * @param string $bundle
   *   Entity bundle.
   * @param string $field_name
   *   Field name.
   *
   * @return string
   *   Page title.
   */
  public function editFormTitle($entity_type, $bundle, $field_name) {
    $instances = $this->entityFieldManager
      ->getFieldDefinitions($entity_type, $bundle);
    if (!isset($instances[$field_name])) {
      return '';
    }
    $field_instance = $instances[$field_name];
    return $field_instance
      ->getLabel();
  }

  /**
   * Title for bundle list of current entity type.
   *
   * @param string $entity_type
   *   Entity type.
   *
   * @return string The title for the bundle list page.
   *   The title for the bundle list page.
   */
  public function bundleListTitle($entity_type) {
    $type = $this->entityTypeManager
      ->getDefinition($entity_type);
    return $type
      ->getLabel();
  }

  /**
   * Show bundle list of current entity type.
   *
   * @param string $entity_type
   *   Entity type.
   *
   * @return array Array of page elements to render.
   *   Array of page elements to render.
   */
  public function bundleList($entity_type) {
    $output = [];
    $bundles = $this->entityTypeBundleInfo
      ->getBundleInfo($entity_type);
    if ($bundles) {
      $output['#theme'] = 'admin_block_content';
      foreach ($bundles as $bundle_key => $bundle) {
        $output['#content'][] = [
          'url' => Url::fromRoute('conditional_fields.conditions_list', [
            'entity_type' => $entity_type,
            'bundle' => $bundle_key,
          ]),
          'title' => $bundle['label'],
        ];
      }
    }
    else {
      $output['#type'] = 'markup';
      $output['#markup'] = $this
        ->t("Bundles not found");
    }
    return $output;
  }

  /**
   * Get list of available EntityTypes.
   *
   * @return ContentEntityTypeInterface[]
   *   List of content entity types.
   */
  public function getEntityTypes() {
    $entityTypes = [];
    foreach ($this->entityTypeManager
      ->getDefinitions() as $key => $entityType) {
      if ($entityType instanceof ContentEntityType) {
        $entityTypes[$key] = $entityType;
      }
    }
    return $entityTypes;
  }

  /**
   * Provide arguments for ConditionalFieldFormTab.
   *
   * @param string $node_type
   *   Node type.
   *
   * @return array
   *   Form array.
   */
  public function provideArguments($node_type) {
    return $this->formBuilder
      ->getForm(ConditionalFieldFormTab::class, 'node', $node_type);
  }

  /**
   * Provide arguments for ConditionalFieldFormTab.
   *
   * @param string $node_type
   *   Node type.
   *
   * @return array
   *   Form array.
   */
  public function getMediaEditFormTab($media_type) {
    return $this->formBuilder
      ->getForm(ConditionalFieldFormTab::class, 'media', $media_type);
  }

  /**
   * Provide arguments for ConditionalFieldFormTab.
   *
   * @param string $node_type
   *   Node type.
   *
   * @return array
   *   Form array.
   */
  public function getBlockEditFormTab($block_content_type) {
    return $this->formBuilder
      ->getForm(ConditionalFieldFormTab::class, 'block_content', $block_content_type);
  }

  /**
   * Provide arguments for ConditionalFieldFormTab.
   *
   * @param string $node_type
   *   Node type.
   *
   * @return array
   *   Form array.
   */
  public function getCommentEditFormTab($comment_type) {
    return $this->formBuilder
      ->getForm(ConditionalFieldFormTab::class, 'comment', $comment_type);
  }

  /**
   * Provide arguments for ConditionalFieldFormTab.
   *
   * @param string $node_type
   *   Node type.
   *
   * @return array
   *   Form array.
   */
  public function getUserEditFormTab() {
    $user_type = "user";
    return $this->formBuilder
      ->getForm(ConditionalFieldFormTab::class, 'user', $user_type);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionalFieldController::$entityFieldManager protected property Entity field manager.
ConditionalFieldController::$entityTypeBundleInfo protected property Entity type bundle info.
ConditionalFieldController::$entityTypeManager protected property The entity type manager. Overrides ControllerBase::$entityTypeManager
ConditionalFieldController::$formBuilder protected property Form Builder. Overrides ControllerBase::$formBuilder
ConditionalFieldController::bundleList public function Show bundle list of current entity type.
ConditionalFieldController::bundleListTitle public function Title for bundle list of current entity type.
ConditionalFieldController::editFormTitle public function Title for field settings form.
ConditionalFieldController::entityTypeList public function Show entity types.
ConditionalFieldController::formTitle public function Title for fields form.
ConditionalFieldController::getBlockEditFormTab public function Provide arguments for ConditionalFieldFormTab.
ConditionalFieldController::getCommentEditFormTab public function Provide arguments for ConditionalFieldFormTab.
ConditionalFieldController::getEntityTypes public function Get list of available EntityTypes.
ConditionalFieldController::getMediaEditFormTab public function Provide arguments for ConditionalFieldFormTab.
ConditionalFieldController::getUserEditFormTab public function Provide arguments for ConditionalFieldFormTab.
ConditionalFieldController::provideArguments public function Provide arguments for ConditionalFieldFormTab.
ConditionalFieldController::__construct public function ConditionalFieldController constructor.
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 40
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.