Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll
19/04/2025, by Ivan

Drupal Documentation

Դաշտերի արժեքները ստանալը էակներից բավականին հեշտ է, բայց կան մի քանի տարբերակներ դա անելու։ Եկեք տեսնենք, թե ինչպես է լավագույնը աշխատել դաշտերի արժեքների հետ սեփական կոդում։ Դուք միշտ կարող եք տեսնել դաշտերի հետ աշխատանքի վերջին տեղեկությունները պաշտոնական կայքում.

https://www.drupal.org/docs/8/api/entity-api/working-with-the-entity-api

Այս հոդվածում մենք կդիտարկենք արժեքների հետ աշխատանքի օրինակներ։ 

Չեք ստիպված հիշել, թե ինչպես է աշխատում այս կամ այն դաշտը․ դուք միշտ կարող եք վերադառնալ այս էջին և հիշել։ Ժամանակի ընթացքում դուք ավելի հաճախ կդիմեք փաստաթղթերին և կհասկանաք, թե որքան հեշտ է դաշտերի հետ աշխատել Drupal-ում։ Մինչ այդ, կարող եք էջը պահել էջանիշերում՝ որպես արագ հղում։

Աշխատանք հանգույցների հետ

Բեռնել հանգույցը ըստ nid-ի՝

$nid = 234; 
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node = $node_storage->load($nid);

Ստանալ հանգույցի ID-ն՝

$node->id();

Ստանալ հանգույցի/էակի bundle-ը՝

$node->bundle();   
$entity->getType(); 

Ստանալ դաշտերի արժեքները՝

$node->get('title')->value;           
$node->get('created')->value;      
$node->get('body')->value;          
$node->get('body')->summary;         
$node->get('field_foo')->value;     
$node->get('field_image')->target_id;

Կարող եք նաև օգտագործել կարճ գրառում՝ արժեքները ստանալու համար՝

$node->title->value;
$node->created->value;
$node->body->value;
$node->body->summary;
$node->field_foo->value;
$node->field_image->target_id;

Բեռնել որոշակի հանգույցներ դաշտի արժեքով՝

$query = \Drupal::entityQuery('node')
  ->condition('type', 'article'),
  ->condition('field_terms', 42);
$nids = $query->execute();
$nodes = $node_storage->loadMultiple($nids);
  
foreach ($nodes as $node) {
  print $node->title->value;
  $node->set('title', "New title for node");
  $node->save();
}

Փոփոխել դաշտի արժեքները՝

$node->set('title', "New title");
$node->set('body', array(
'summary' => "Teaser",
'value' => "Long text",
'format' => 'basic_html',
));
$node->save();

Եվ եթե դաշտը միայն մեկ արժեք ունի, կարող եք օգտագործել կարճ գրառում՝

$node->title = 'New title';
$node->field_text = 'text';

Ստանալ մի քանի դաշտի արժեքները՝

$nids = \Drupal::entityQuery('node')->condition('type', 'album')->execute();
$nodes = Node::loadMultiple($nids);
 
$data = array();
foreach($nodes as $node) {
  $photo = array();
  foreach($node->get('field_image')->getValue() as $file){
    $fid = $file['target_id']; // ստանալ ֆայլի fid;
    $photo[] = \Drupal\file\Entity\File::load($fid)->getFileUri();
  }
 
  $data[] = array(
    'album_name' => $node->get('field_album_name')->getValue(),
    'place' => $node->get('field_place')->getValue(),
    'author' => $node->get('field_author')->getValue(),
    'photo' => $photo,
  );
}

Աշխատանք ֆայլային դաշտերի հետ

Ֆայլերը կցվում են այլ էակներին հղման դաշտերի միջոցով, և երբ մենք մուտք ենք գործում այդ դաշտերին, կարող ենք ստանալ ֆայլի ID-ն և հետո ստանալ ֆայլի տվյալները։

Ստանալ ֆայլ ըստ ID-ի՝

$fid = 42; 
$file_storage = \Drupal::entityTypeManager()->getStorage('file');
$file = $file_storage->load($fid);

Ստանալ ֆայլի օբյեկտը հանգույցի դաշտից՝

$file = $node->field_image->entity;

Ստանալ ֆայլի օբյեկտի որոշ դաշտեր՝

$file->getFileUri();   // "public://file123.jpg"
// URI-ն դարձնել ֆայլի URL՝ file_url_transform_relative(file_create_url($file->getFileUri()));   
// "/sites/default/files/public/file123.jpg"
$file->filename->value;   // "file123.jpg"
$file->filemime->value;   // "image/jpeg"
$file->filesize->value;   // 63518  (բայթերում չափսը)
$file->created->value;    // 1511206249  (Unix timestamp)
$file->changed->value;    // 1511234256  (Unix timestamp)
$file->id();

Կարող եք տեսնել file_managed աղյուսակից հասանելի հատկությունները՝

echo $file->uid->target_id;               // 1
echo $file->uid->value;                   // Չի աշխատում։ Օգտագործեք target_id։
echo $file->uid->entity->name->value;    
echo $file->uid->entity->timezone->value; // "Asia/Omsk"

Աշխատանք էակի հղման դաշտերի հետ

Կարող եք ստանալ մի քանի արժեք՝ foreach ցիկլով մշակելու համար՝

foreach ($node->field_my_entity_reference as $reference) {  
  print $reference->target_id;  
  print $reference->entity->title->value;  
}

Փոփոխել էակի հղման բազմակի դաշտ՝

$nids = [3,4,5,6];  
$node->set('field_my_entity_reference', $nids);
$node->save();

Ավելացնել նոր արժեքներ՝ պահպանելով արդեն եղածները՝

$nids = [3,4,5,6];   // օրինակ արժեքներ
foreach ($nids as $nid) {
  $node->field_my_entity_reference[] = [
    'target_id' => $nid
  ];
}
$node->save();

Աշխատանք paragraph-ների հետ

$my_paragraph = null;
  
foreach ($node->get('field_paragraph_reference') as $paragraph) {
  if ($paragraph->entity->getType() == 'your_paragraph_type') {  
    $my_paragraph = $paragraph->entity;
  }
}
  
if (!empty($my_paragraph)) {
  print $my_paragraph->field_somefield->value;
  
  print $my_paragraph->title->value;  // Չի աշխատում։
} 
else {
  print "Հանգույցը չունի այս տեսակի paragraph։";
}

Ստանալ paragraph-ի տեսակը՝

$my_paragraph->getType();

Մենք ապագայում կօգտագործենք այս օրինակները՝ գրելիս մոդուլներ, որոնք աշխատում են hook-երի և էակների օբյեկտների հետ։