You are here

class EditableFieldsHelper in Editable Fields 1.0.x

Same name and namespace in other branches
  1. 8 src/services/EditableFieldsHelper.php \Drupal\editablefields\services\EditableFieldsHelper

Class EditableFieldsHelper.

Helper service for the "editablefields" functionality.

Hierarchy

Expanded class hierarchy of EditableFieldsHelper

1 string reference to 'EditableFieldsHelper'
editablefields.services.yml in ./editablefields.services.yml
editablefields.services.yml
1 service uses EditableFieldsHelper
editablefields.helper in ./editablefields.services.yml
Drupal\editablefields\services\EditableFieldsHelper

File

src/services/EditableFieldsHelper.php, line 17

Namespace

Drupal\editablefields\services
View source
class EditableFieldsHelper implements EditableFieldsHelperInterface {

  /**
   * Drupal\Core\Field\FieldTypePluginManagerInterface definition.
   *
   * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
   */
  protected $pluginManagerFieldFieldType;

  /**
   * Drupal\Core\Form\FormBuilderInterface definition.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * Drupal\Core\DependencyInjection\ClassResolverInterface definition.
   *
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
   */
  protected $classResolver;

  /**
   * Drupal\Core\Session\AccountProxyInterface definition.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * Drupal\Core\Entity\EntityDisplayRepositoryInterface definition.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $entityDisplayRepository;

  /**
   * Constructs a new EditableFieldsHelper object.
   */
  public function __construct(FieldTypePluginManagerInterface $plugin_manager_field_field_type, FormBuilderInterface $form_builder, ClassResolverInterface $class_resolver, AccountProxyInterface $current_user, EntityDisplayRepositoryInterface $entity_display_repository) {
    $this->pluginManagerFieldFieldType = $plugin_manager_field_field_type;
    $this->formBuilder = $form_builder;
    $this->classResolver = $class_resolver;
    $this->currentUser = $current_user;
    $this->entityDisplayRepository = $entity_display_repository;
  }

  /**
   * {@inheritDoc}
   */
  public function formatterInfoAlter(array &$info) {
    if (empty($info[self::FORMATTER_ID])) {
      return;
    }
    $info[self::FORMATTER_ID]['field_types'] = $this
      ->getAllFieldTypes();
  }

  /**
   * {@inheritDoc}
   */
  public function getAllFieldTypes() {
    return array_keys($this->pluginManagerFieldFieldType
      ->getDefinitions());
  }

  /**
   * {@inheritDoc}
   */
  public function checkAccess(EntityInterface $entity) {
    $can_edit = $entity
      ->access('edit');
    $can_use = $this->currentUser
      ->hasPermission(self::PERMISSION);
    return $can_edit && $can_use;
  }

  /**
   * {@inheritDoc}
   */
  public function isAdmin() {
    return $this->currentUser
      ->hasPermission(self::ADMIN_PERMISSION);
  }

  /**
   * {@inheritDoc}
   */
  public function getForm(EntityInterface $entity, string $field_name, string $form_mode) {

    /** @var \Drupal\editablefields\Form\EditableFieldsForm $form_object */
    $form_object = $this->classResolver
      ->getInstanceFromDefinition(self::FORM_CLASS);
    $form_object
      ->setDefaults($entity, $field_name, $form_mode);
    return $this->formBuilder
      ->getForm($form_object);
  }

  /**
   * {@inheritDoc}
   */
  public function getFormDisplay(EntityInterface $entity, $form_mode) {
    return $this->entityDisplayRepository
      ->getFormDisplay($entity
      ->getEntityTypeId(), $entity
      ->bundle(), $form_mode);
  }

  /**
   * {@inheritDoc}
   */
  public function getModesOptions() {
    $options[self::DEFAULT_FORM_MODE] = self::DEFAULT_FORM_MODE;
    $form_modes = $this->entityDisplayRepository
      ->getAllFormModes();

    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface[] $modes */
    foreach ($form_modes as $entity_type => $modes) {
      foreach ($modes as $mode) {
        $label = explode('.', $mode['id']);
        $label = end($label);
        $options[$entity_type][$label] = $label;
      }
    }
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EditableFieldsHelper::$classResolver protected property Drupal\Core\DependencyInjection\ClassResolverInterface definition.
EditableFieldsHelper::$currentUser protected property Drupal\Core\Session\AccountProxyInterface definition.
EditableFieldsHelper::$entityDisplayRepository protected property Drupal\Core\Entity\EntityDisplayRepositoryInterface definition.
EditableFieldsHelper::$formBuilder protected property Drupal\Core\Form\FormBuilderInterface definition.
EditableFieldsHelper::$pluginManagerFieldFieldType protected property Drupal\Core\Field\FieldTypePluginManagerInterface definition.
EditableFieldsHelper::checkAccess public function Checks if the user can use "editablefields" formatter. Overrides EditableFieldsHelperInterface::checkAccess
EditableFieldsHelper::formatterInfoAlter public function Make "editablefields" formatter available for all the field types. Overrides EditableFieldsHelperInterface::formatterInfoAlter
EditableFieldsHelper::getAllFieldTypes public function Get machine names of all the field types. Overrides EditableFieldsHelperInterface::getAllFieldTypes
EditableFieldsHelper::getForm public function Prepares a render array of the editable field form. Overrides EditableFieldsHelperInterface::getForm
EditableFieldsHelper::getFormDisplay public function Loads entity form display. Overrides EditableFieldsHelperInterface::getFormDisplay
EditableFieldsHelper::getModesOptions public function Helper method to prepare the list of the form modes. Overrides EditableFieldsHelperInterface::getModesOptions
EditableFieldsHelper::isAdmin public function Check if the user has administer permission. Overrides EditableFieldsHelperInterface::isAdmin
EditableFieldsHelper::__construct public function Constructs a new EditableFieldsHelper object.
EditableFieldsHelperInterface::ADMIN_PERMISSION constant Editablefields admin permission.
EditableFieldsHelperInterface::DEFAULT_FORM_MODE constant Default form mode.
EditableFieldsHelperInterface::FORMATTER_ID constant Editablefields formatter ID.
EditableFieldsHelperInterface::FORM_CLASS constant Editablefields form class.
EditableFieldsHelperInterface::PERMISSION constant Use "editablefields" permission.