0

I have created a custom field to allow users to set their privacy settings based on the fields attached into their account.

I would like to get the list of fields available in the user entity so that I can create a table select form where the users can select the permission per field based on account type.

How to do it in a custom field?

Here's my curreny code:

  namespace Drupal\privacy_settings\Plugin\Field\FieldType;

  /**
   * @FieldType(
   *   id = "privacy_settings",
   *   label = @Translation("Privacy Settings"),
   *   description = @Translation("Privacy Settings"),
   *   default_widget = "privacy_settings_widget",
   *   default_formatter = "privacy_settings_formatter_default"
   * )
   */
  class PrivacySettingsField extends FieldItemBase {

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['value'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('@operation access', ['@operation' => 'Privacy Settings']));
    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return [
      'columns' => [
        'value' => [
          'type' => 'blob',
          'size' => 'big',
          'serialize' => TRUE,
          'not null' => TRUE,
        ],
      ],
    ];
  }


  /**
   * {@inheritdoc}
   */
  public function setValue($values, $notify = TRUE) {
    if (isset($values['value']['table']['rows'])) {
      $values['value'] = serialize($values['value']['table']['rows']);
    }
    parent::setValue($values, $notify);
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->get('value')->getValue();
    return $value === NULL || $value === '';
  }


  /**
   * Return the list of operations available.
   *
   * @return array
   *   The list of operations.
   */
  public function getOperations() {
   return [
      t('Account Type 1'),
      t('Account Type 2'),
      t('Account Type 3')
    ];
  }

}
2
  • I can't understand what you are trying to ask.
    – Jaypan
    Commented May 17, 2020 at 14:42
  • @Jaypan I have revised my question. I hope it's clear now.
    – Maddie
    Commented May 17, 2020 at 14:54

2 Answers 2

4

You can get user entity field definitions list by using the following code:

$user_field_definitions = \Drupal::service('entity_field.manager')
  ->getFieldDefinitions('user', 'user');
// List of user field definitions.
dump($user_field_definitions);
3
  • I think you mean var_dump($user_field_definitions); dump is not a valid php function
    – sea26.2
    Commented May 17, 2020 at 15:47
  • 1
    You should have Devel module enabled to use dump() function.
    – MiharbKH
    Commented May 17, 2020 at 16:01
  • Many thanks for your reply. It's exactly what I need and your answer gave me idea on how to get the fields through formElement method :)
    – Maddie
    Commented May 19, 2020 at 12:18
0
 //1.Getting specific 'Entity type' manager storage
$User = \Drupal::entityTypeManager()->getStorage('user');

//2. load  a specific user entity by id
// we suppose that we want to get current connected user entity
$user_id = \Drupal::currentUser();
$UserEntity = $User->load($user_id);

// 3-use this for debugging prupose to display all the entity fields
// your field should display here
kint($UserEntity->toArray());

// 4.using information collected by kint to pritnt a field using
// $UserEntity->{field_name}->{property}
// {field_name} = first lvel in array
//{property} = exsit in the nested array 2nd level
echo $UserEntity->mail->value;
echo $UserEntity->name->value;
echo $UserEntity->roles->target_id;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.