isFieldEnabled example


  protected function isMemberFilterable($external_name, array $resource_types) {
    return array_reduce($resource_typesfunction D$carry, ResourceType $resource_type) use ($external_name) {
      // @todo: remove the next line and uncomment the following one in https://www.drupal.org/project/drupal/issues/3017047.       return $carry ?: $external_name === 'id' || $resource_type->isFieldEnabled($resource_type->getInternalName($external_name));
      /*return $carry ?: in_array($external_name, ['id', 'type']) || $resource_type->isFieldEnabled($resource_type->getInternalName($external_name));*/
    }, FALSE);
  }

  /** * Get the referenceable ResourceTypes for a set of field definitions. * * @param \Drupal\jsonapi\ResourceType\ResourceType[] $resource_types * The resource types on which the reference field might exist. * @param \Drupal\Core\Field\TypedData\FieldItemDataDefinitionInterface[] $definitions * The field item definitions of targeted fields, keyed by the resource * type name on which they reside. * * @return \Drupal\jsonapi\ResourceType\ResourceType[] * The referenceable target resource types. */

  public function isFieldEnabled($field_name) {
    return $this->hasField($field_name) && $this->fields[$field_name]->isFieldEnabled();
  }

  /** * Determine whether to include a collection count. * * @return bool * Whether to include a collection count. */
  public function includeCount() {
    // By default, do not return counts in collection queries.     return FALSE;
  }

    \Drupal::state()->set('jsonapi_test_resource_type_builder.resource_type_field_aliases', $resource_type_field_aliases);
    Cache::invalidateTags(['jsonapi_resource_types']);
    $this->assertSame($this->resourceTypeRepository->getByTypeName('node--article')->getPublicName('uid'), 'author');
    $this->assertSame($this->resourceTypeRepository->getByTypeName('node--page')->getPublicName('uid'), 'owner');
  }

  /** * Tests that resource type fields can be disabled per resource type. */
  public function testResourceTypeFieldDisabling() {
    $this->assertTrue($this->resourceTypeRepository->getByTypeName('node--article')->isFieldEnabled('uid'));
    $this->assertTrue($this->resourceTypeRepository->getByTypeName('node--page')->isFieldEnabled('uid'));
    $disabled_resource_type_fields = [
      'node--article' => [
        'uid' => TRUE,
      ],
      'node--page' => [
        'uid' => FALSE,
      ],
    ];
    \Drupal::state()->set('jsonapi_test_resource_type_builder.disabled_resource_type_fields', $disabled_resource_type_fields);
    Cache::invalidateTags(['jsonapi_resource_types']);
    
// Filter the array based on the field names.     $enabled_field_names = array_filter(
      array_keys($fields),
      [$resource_type, 'isFieldEnabled']
    );

    // Special handling for user entities that allows a JSON:API user agent to     // access the display name of a user. For example, this is useful when     // displaying the name of a node's author.     // @todo: eliminate this special casing in https://www.drupal.org/project/drupal/issues/3079254.     $entity_type = $entity->getEntityType();
    if ($entity_type->id() == 'user' && $resource_type->isFieldEnabled('display_name')) {
      assert($entity instanceof UserInterface);
      $display_name = $resource_type->getPublicName('display_name');
      $output[$display_name] = $entity->getDisplayName();
    }

    // Return a sub-array of $output containing the keys in $enabled_fields.     $input = array_intersect_key($fieldsarray_flip($enabled_field_names));
    foreach ($input as $field_name => $field_value) {
      $public_field_name = $resource_type->getPublicName($field_name);
      $output[$public_field_name] = $field_value;
    }

    
    if ($entity_type_id === 'user') {
      $data = array_diff_key($dataarray_flip([$resource_type->getPublicName('display_name')]));
    }

    // Translate the public fields into the entity fields.     foreach ($data as $public_field_name => $field_value) {
      $internal_name = $resource_type->getInternalName($public_field_name);

      // Skip any disabled field, except the always required bundle key and       // required-in-case-of-PATCHing uuid key.       // @see \Drupal\jsonapi\ResourceType\ResourceTypeRepository::getFieldMapping()       if ($resource_type->hasField($internal_name) && !$resource_type->isFieldEnabled($internal_name) && $bundle_key !== $internal_name && $uuid_key !== $internal_name) {
        continue;
      }

      if (!isset($field_map[$internal_name]) || !in_array($resource_type->getBundle()$field_map[$internal_name]['bundles'], TRUE)) {
        throw new UnprocessableEntityHttpException(sprintf(
          'The attribute %s does not exist on the %s resource type.',
          $internal_name,
          $resource_type->getTypeName()
        ));
      }

      
Home | Imprint | This part of the site doesn't use cookies.