8 – How can I limit an entity reference selection to a specific field value on the reference

I have a content type Research which has a an entity reference type of People.

The problem is that I don’t want to show ALL people but only people which have a certain value under their field named type.
I was trying to find it here:
enter image description here

Anyone an idea how to only show a content-type for a certain field value of that content type

8 – How to put view results in an entity reference field?

So the scenario is like this:
I have to create a “node” which has an entity reference field which can hold multiple values. I am looking for a way to be able to select view results to be stored in the entity reference field and I do not know how to do that in Drupal 8.

For Drupal 7 there was this module which does what I need https://www.drupal.org/project/entityreference_view_widget

I have tried doing it the custom way by creating views and placing them on the same page as the node create and then getting the resulting values with ajax to store in the field but I do not believe that is the best way to go about it as it does not seem reliable.

Any advice is appreciated.

8 – Placing a block on an entity

I am aiming to place a block on an entity.

It seems like this should be simple as entity integration in D8 is well-developed and (in parts) user-friendly.

I can get the view block working fine and can ‘Place Block’ on different content types successfully.

However when it comes to entities there are no options in the ‘Configure Block: Visibility’ tabs.

Perhaps there is some standard documentation on customising the appearance of entities in this way that I just cant seem to find?

D8 seems to have integrated entities quite well with Views etc. But somehow simply placing a block on an entity remains a challenge.

I expect other relatively less experienced users would be experiencing the same challenges.

Perhaps I am missing something very obvious. If so perhaps someone could let me know! 🙂

Thoughts and guidance welcome.

8 – Can entity references have “types” like links in Jira?

Suppose I have a node that represents a task, or a to-do item:

Node 1: Instructions for building a drivable car

I want to link this node to another node, such as a sub-task of the original node:

Node 2: Instructions for assembling an engine (Sub-task of Node 1)

Now suppose I have another node that is caused by one of my original nodes, but is not a sub-task of it.

Node 3: How to obtain a driver license (required by Node 1)

The entity reference from Nodes 2 to 1 is of type sub-task while the link from 3 to 1 is of type required by (or maybe related to, whatever).

When viewing Node 1, it might be helpful to display a view of referenced nodes, but rather than just showing the nodes, it would be useful to show the relationship type as well:

Items related to "Instructions for building a drivable car":

| Link Type      | Node                                  |
|--------------------------------------------------------|
| Parent Task of | Instructions for assembling an engine |
| Requires       | Getting a driver license              |

Applications such as Jira handle this via link types. Is functionality like this available within the Drupal ecosystem? Alternatively, what would be a logical way of implementing such functionality?

json – Unprocessable Entity: The following relationship fields were provided as attributes

As the error indicates, the entity reference has to be set in relationships, not attributes.

The documentation gives this example:

{
  "data": {
    "type": "node--article",
    "attributes": {
      "title": "Article by admin",
      "body": {
        "value": "Custom value",
        "format": "plain_text"
      }
    },
    "relationships": {
      "uid": {
        "data": {
          "type": "user--user",
          "id": "{{UUID of user 1}}"
        }
      }
    }
  }
}

In this example, the author of the node is set using the relationship of uid. However, it wasn’t immediately clear to me how to set the entity reference field.

It turns out to be quite easy:

const data = {
  data: {
    type: 'node--page',
    attributes: {
      field_text: {
        value: `${values.textFieldValue}`,
        format: 'basic_html',
      },
    },
    relationships: {
      field_ref_term: {
        data: {
          type: 'taxonomy-term--my-vocabulary',
          id: '9fa0cdc7-384c-4aa4-8ce9-d6c97fc2f44b',
        },
      },
    },
  },
};

The key for the relationship is the machine name of the entity reference field.

9 – View to get information for the user entity reference using contextual filters

I have two content types and each one has a user entity reference field. I want a view using contextual filter that shows the information of the content types when I pass the user ID referenced for the user entity reference field.

content-type 1: field -> studies, field -> id-user-entity-reference
content-type 2: field -> experience, field -> id-user-entity-reference

I want to get for example studies and experience in a view when I have a path similar like that:

/content/id-user-entity-reference (not the logged user, not the author id of the content type)

I need these results can be exposed via rest.

How can I configure my view to achieve that?

8 – identify author of an entity

Afaik, there’s no requirement that a Content Entity must store the id of the user who created/edited it. However, many entity types do use a common method to store the owner information – Drupal core’s user module provides an EntityOwnerTrait that many entity types use. (At least Node, Media, etc, all use this trait.)

For example, if you’re looking for the owner of an entity that uses this trait, you can use the getOwner() method like:

$owner_user_object = $entity->getOwner();

It’s unclear if user U2, in your example, would be the owner of the entity or just the author of a particular revision. If your entity is storing revision information, and the entity implements EditorialContentEntityBase (Note that they probably do. Node, Media, etc, all implement this base class) then it’s also possible to load the author of a particular revision.

First, load the revision. See @baikho’s comment pointing to Preferred way of loading a specific entity revision.

$entity_revision = Drupal::entityTypeManager()
  ->getStorage('MY_ENTITY_EYPE')
  ->loadRevision($MY_REVISION_ID);

Once you’d loaded the revision, you can get the author of that revision with the getRevisionUser() method like:

$revision_user_object = $entity_revision->getRevisionUser()

c# – In the absence of code-contracts, what techniques exist to represent state invariants (e.g. “Customer with Orders loaded” with Entity Framework)?

Indirection

There is a significant difference between an entity and a viewmodel. You’ve already pointed one out: entities have no inherent requirements of linked entities to exist/be loaded, but in this case your viewmodel does.
Since your viewmodel is the one who causes this requirement to exist, putting it in the viewmodel is precisely the place it should be enshrined.

but this introduces a lot of time wasting by essentially copying the data’s structural definition from the Entity type to the View-Model type

This is the age-old mantra of bad practice justifications. I want to cut this corner because I it’s a waste of time.

Not only should you take the time and effort to separately declare the structure of an entity and a viewmodel, regardless of any similarities they may share; but you also seem to have skipped one or two extra layers of indirection, i.e. the database DTO and business DTO.

This is where we get to differing opinions on where the bar is set. Some advocate that you always need all layers. Some advocate a more contextual approach. But I think you’ll get a near unanimous agreement that entities and viewmodels are the absolute barebones requirements for indirection. I would personally suggest having at least one additional layer of indirection in there, but that’s a contextual inference on my part about the scope and complexity of your application.

if the View Model class simply embeds Entity Types (which is fine when the View Model is not being used in a <form>!)

I have no idea where you got this idea, I have never heard of this argument before, and I disagree with it wholly.

Whether or not your data ends up in a form is completely irrelevant. Any data you send to a browser gets sent as HTML (since you’re talking about <form>) and inherently loses all connection to the entity that was the source of the data.

If the user ends up making a second request, this time containing some form data that just happens to be the same as the entity you used for the first request, that’s a completely separate request, with its own scope, and no bearing on the entity from the first event.

Note that a slightly different argument applies to non-HTML formats, e.g. a WPF app; but it’s the same conclusion in the end. Here, at the end of a rendering a view the entity does not necessarily descope yet (because it can and most likely will be kept in memory). However, the only way in which your rendered view impacts the entity is if you set up a binding. And it makes no sense to set up the binding for the content of an immutable object. If it’s immutable, then you wouldn’t be able to reflect any changes made to the data anyway.

which doesn’t “scale” when an application could have hundreds of different views all with similar requirements

We’ve now hit on the second mantra of bad practice justification. I could decide to not cut this corner, but there are many corners ahead, and not cutting any corner is a waste of time.

Every class exists because it has its own defined purpose. Different purpose? Different class. Same purpose? Same class.

Note the difference between a class’ purpose and its structure. Two classes may exist with the exact same class structure, but each serving a different purpose (DTOs are a common example here). Overzealous refactoring may lead you to condense these two classes together as their structure is the same, but that is a bad practice mistake. Each class serves its own purpose and therefore each class has its own reason to exist.

The size of an application is not justification for cutting corners. There is not a single good practice rule that has a “unless you have a lot of classes” kind of exception to it.

Just imagine if you find out that the contractor building your house skipped parts of the building plan “because they have a lot of houses to build”. The building plan is to be followed, regardless of how many building plans exist.
“I had to do many” is just not a valid argument. But often, we only care about that when we personally suffer from the corner-cutting, rather than when we’re the one doing it for our own benefit (i.e. less work).


Immutability

And this immutable View Model for a page

class CustomerOrderPageViewModel : IPageViewModel
{
    public Customer CustomerSubject { get; }
}

If Customer is mutable, then given its class definition, CustomerOrderPageViewModel is also mutable, since you can do things like this:

var model = new CustomerOrderPageViewModel(new Customer);

model.CustomerSubject.Name = "I can totally change this";

Immutability can only be built on immutability. An interesting discussion appears when you consider if Customer had not been publically accessible from the viewmodel, e.g.:

class CustomerOrderPageViewModel : IPageViewModel
{
    private readonly Customer customer;
    public Customer CustomerName => this.customer.Name;
}

Is this immutable? I’ve heard argument either way. You cannot alter a CustomerOrderPageViewModel instance. However, it’s still possible to indirectly alter the data contained in this CustomerOrderPageViewModel instance:

var customer = new Customer();
var model = new CustomerOrderPageViewModel(customer);

var modelName_1 = model.CustomerName;

customer.Name = "Different";

var modelName_2 = model.CustomerName;

If the same property of the same instance can yield different values, then the object is by definition mutable.

This answer explores the issue better than I can, but the gist of it is that the only way to have an immutable class that is based on a mutable reference type (i.e. the entity) is to (a) obviously not expose any mutable types as properties and (b) copy all the needed data in the constructor, so that any future changes made to the instance of the mutable reference type no longer cause changes to the content of your immutable instance.

Note that when you do actually implement the indirection described above, and your viewmodels and entities are separate, then you can in fact achieve immutability easily here. I just wanted to point out that what you call immutability is not in fact immutability, because you are liable to repeat that mistake even if you were to add a layer of indirection.

entities – add Bundle field to a custom entity

I am trying to add a new bundle field into my custom entity, I added the following code into the MyCustomEntity.php file, I uninstalled my module, so, I installed it again, the field is showing up on the view, but in my data base the field is not added.

Could you please support me?

$fields('new_name') = BaseFieldDefinition::create('text_long')
  ->setLabel(t('New Name'))
  ->setDisplayOptions('form', (
    'type' => 'text_textarea',
  ))
  ->setRevisionable(TRUE)
  ->setTargetBundle($bundle);

Thank you, I will really appreciate your support.

identity – Entity Relationship Model

Draw an ER-Model for the following info that can appropriately record the info described :

Question : An Airport has a city name and International identification code (Eg: EDDH for Hamburg Airport). A fight is identified by its flight no (Eg: LH4324). A flight has a starting airport, a destination airport and a departure date. Further more, a fight has three prices (first, business, economy) and a status (not yet started, started, landed).

Can anyone on this forum solve this question for me. It will be grateful for me.