How can I see all the places an asset or entity is referenced?

In Drupal 8, I have a requirement from a client that they be able to find a reference of a given asset (file, node, entity, etc) throughout a Drupal site. The ultimate goal is to know that if they want to delete this asset, it’s not going to create 404 errors throughout the places on the site where this is referenced in other content.

I’m thinking about a Views page where the name or ID or some type of identifier would be a contextual filter and then would show a list of all nodes or blocks where that relationship exists.

Any thoughts on how to approach solving an issue like this?

How to print an entity bundle field from one with multiple values

Struggling to find the answer to this. I have an entity called “school objects” which has a bundle called “homepage”. One of the fields attached to this is called “Hero banners” and it allows multiple text values.

Example

All I want to do is print each value. I don’t need to check for conditions, I don’t need NIDs, I just want each value. I am not really a php developer but understand the basics of what it is doing. I was looking into the EntityFieldQuery code, but cannot get anything to print on a tpl file I am using.

The output I need, based on image attached, is simply:

<div class="value_1">images/symbol.png</div> <div class="value_2">Let's make a difference</div> <div class="value_3">Start today</div> 

All I can find after a lot of searching is complex bits of code checking for conditions and stuff that I do not need. I need some kind of array I think so that my template looks like:

<div class="value_1">Value[0]</div> <div class="value_2">Value[1]</div> <div class="value_3">Value[2]</div> 

All I got so far is this:

query = new EntityFieldQuery(); $  query->entityCondition('entity_type', 'school_objects')   ->entityCondition('bundle', 'homepage')   ->range(0, 14); $  result = $  query->execute(); 

I have read the link to this on the Drupal site but cannot understand it or how to print what I need.

Migrating a file url to media entity attached to a content type

I am migrating content from a JSON API. One of the attribute fields includes a PDF file URL.

How can I add this to the Migration definition YAML to take this url, save the file, generate a media entity and attach it to the node I am importing all at once?

Note, I have 0 way of getting files ahead of time, so that approach is a no-go. I need to do this as I am processing the main content.

I tried this based on some of the things I have seen:

  field_paper_working_paper:     target_id:       -         plugin: file_copy         source:           - doc_format_url           - 'public://working_papers'       -         plugin: entity_generate         value_key: uri         entity_type: media 

But I get:

The "" plugin does not exist. 

O que são os termos Weak Entity, Weak Relationship e One Optional/Mandatory?

Estou usando uma ferramento online yEd Live para modelar um diagrama MER. Entretanto, há alguns elementos que usam uns termos no qual me deixam com algumas dúvidas.

Esses termos são:

  • WeakEntity (retângulo)
  • WeakRelationship (losango)
  • One Optional (seta para representar cardinalidade)
  • One Mandatory (seta para representar cardinalidade)

Pergunta

Portanto, eu gostaria de saber o que são esses termos e quais as diferenças entre eles?

Basefield ‘string’ shown as select list widget in entity form

I’ve got a custom entity (‘content_sharing’) and defined one of the fields as $ fields['entity_definition'] = BaseFieldDefinition::create('string').... The create-form I’m showing the user is loaded with an empty entity via a controller like so:

    $  contentSharing = $  this->entityTypeManager()       ->getStorage('content_sharing')       ->create([]);     $  form = $  this->entityTypeManager()       ->getFormObject('content_sharing', 'custom_mode')       ->setEntity($  contentSharing);     $  output['form'] = $  this->formBuilder()->getForm($  form);     ... 

What I need now is to present the user a select-list of possible strings (options) that are dynamically pre-populated everytime the form is loaded. The only FieldWidget for string fields is a textfield. I tried to come up with my own custom FieldWidget similar to this

 * @FieldWidget(  *   id = "string_select_list",  *   module = "content_sharing",  *   label = @Translation("Select list for strings"),  *   field_types = {  *     "string"  *   },  *  multiple_values = FALSE  * )  */  class StringSelectList extends OptionsWidgetBase { 

However, this resulted in an error and I noticed that this needs to have options defined which I cannot (don’t know how to) provide for my string basefield.

I could of cource just add a dummy form-element select list that provides the values for my entity field, but I wanted to use the entityform as it is without any further custom elements.

Possible with a custom widget or any other solutions?

Preuso/empty entity or only real entities + nulls?

I have a table “products” and a child one “product_modifications”

Some products have “product_modifications”: “colour”, “size”,, …. But not all.

When a client orders a product, views pictures and other things, he might do that with either product or its modifications, depending on whether or not a particular product is a pure one or a product with modifications.

Question: how do make it more effortless and flexible?

Should I keep reffering to either “products” or “product_modifications” when creating orders, calculating prices, and so on…. depending on whether or not a product has modification?

Or should I always create an empty/default “product_modifications” for each product and work only with “product_modifications”?

How to register data mapper for entity?

If switch on class type is harmful what is a solution to register data mapper without switch operator?
In this case switch also harmful? What if we’ll forget modify DataMapperFactory after we add new Entity? I mean we should know where to add new DataMapper

class Order {     public $  id;     public $  products = [];      public function add(Product $  product)     {         $  this->products[] = $  product;     } }  class OrderDataMapper {     public function save(Order $  order)     {         //....     } }  class DataMapperFactory {     public static function fromEntity($  entity)     {         switch (get_class($  entity->class)) {             case Order::class:                 return new OrderDataMapper();             default:                 return new DefaultDataMapper();         }     } }  $  order = new Order(); $  order->add($  product); $  datamapper = DataMapperFactory::fromEntity($  order); $  datamapper->save($  order); 

Setting the default value of an entity form radio button using hook_form_FORMID_alter

I have written a custom Drupal 7 module which needs to set the value of a radio button of an entityform based on a value queried from the database. I have tested hook and it is working as i am able to set a text field with in the form using:

$  form['field_sem_submitter_id']['und'][0]['value']['#value'] = $  user->uid; 

I have tried, to no avail, to set the radio using the following and various other suggestions:

$  form['field_sem_pledge_currency']['und'][0]['value']['#value'] = '2'; $  form['field_sem_pledge_currency']['und'][0]['value']['#value'] = 'currency_bitcoin'; v$  form['field_sem_pledge_currency']['#default_value'] = 'currency_bitcoin'; 

I have searched and tried numerous other suggestions on the web with no success. Any suggestions greatly appreciated.

How a media image entity_reference field in a custom content entity can be set to translatable?

I created a media image field in my custom entity content:

$  fields['image'] = BaseFieldDefinition::create('entity_reference')   ->setLabel(t('Promotion image'))   ->setDescription(t('Recommended image size is 1600x900 pixels.'))   ->setRevisionable(TRUE)   ->setTranslatable(TRUE)   ->setRequired(FALSE)   ->setSetting('target_type', 'media')   ->setSetting('handler', 'default:media')   ->setSettings([     'handler_settings' => [       'target_bundles' => [         'image' => 'image',       ],       'sort' => [         'field' => '_none',       ],       'auto_create' => FALSE,       'auto_create_bundle' => '',     ],   ])   ->setCardinality(1)   ->setDisplayConfigurable('form', TRUE)   ->setDisplayConfigurable('view', TRUE)   ->setDisplayOptions('form', [     'settings' => [       'entity_browser' => 'image_browser',       'field_widget_display' => 'rendered_entity',       'field_widget_edit' => TRUE,       'field_widget_remove' => TRUE,       'open' => TRUE,       'selection_mode' => 'selection_append',       'field_widget_display_settings' => [         'view_mode' => 'default',       ],       'field_widget_replace' => FALSE,     ],     'type' => 'entity_browser_entity_reference',     'weight' => 3,   ])   ->setDisplayOptions('view', [     'label' => 'hidden',     'settings' => [       'target_type' => 'media',     ],     'weight' => 3,   ]); 

Even though the entity itself, and the field are set to translatable, the ALT and TITLE attributes of the image are not translated.

If I create this field manually in the UI, it works as intended, and gets translated. How could I define this field to be translatable programmatically?