Last active
December 6, 2021 11:03
-
-
Save brain2xml/725c80726bb0c28de3abc67b09b36192 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ======= composer ============ | |
# install | |
composer create-project drupal/recommended-project:^8 project_name | |
# install module | |
composer require drupal/module_name | |
# update drupal | |
composer update | |
php vendor/bin/drush updatedb | |
# ======== console ============ | |
# install module | |
composer require drupal/console | |
# site dev mode | |
vender/drupal/console/bin/drupal site:mode dev | |
# site prod mode | |
vender/drupal/console/bin/drupal site:mode prod | |
# ======= drush =============== | |
composer require drush/drush | |
php vendor/bin/drush scr somefile.php | |
# ====== log ================== | |
\Drupal::logger('my_module')->notice($message); | |
\Drupal::logger('content_entity_example')->notice('@type: deleted %title.', | |
array( | |
'@type' => $this->entity->bundle(), | |
'%title' => $this->entity->label(), | |
)); | |
# ======== users ============== | |
# create user | |
\Drupal\user\Entity\User::create([ | |
'mail' => $user[0], | |
'name' => $user[1], | |
])->save(); | |
# current user | |
$user = \Drupal::currentUser(); | |
# get user by id | |
$user = User::load($user_id); | |
# all useres with role | |
$admin_user_ids = \Drupal::entityQuery('user') | |
->condition('roles', 'Administrator', 'CONTAINS') | |
->execute(); | |
# load by properties | |
$users = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties(['field_region'=>$tids]); | |
# check permission | |
$user->hasPermission('some_permission') | |
# check role | |
$user->hasRole('some_role') | |
# check if user | |
if($user instanceof \Drupal\user\UserInterface) | |
# att | |
->id(); | |
->getUsername(); | |
->getRoles(); | |
->getEmail(); | |
$user = user_load_by_name('some user'); | |
$uid = $user->id(); | |
$user = user_load_by_mail('[email protected]'); | |
$uid = $user->id(); | |
# ======== nodes ============== | |
# load nodes by id | |
$node = \Drupal\node\Entity\Node::load($nid); | |
# load nodes by parametrs | |
$checklists = \Drupal::entityTypeManager()->getStorage('node') | |
->loadByProperties(['type' => 'node_type_name', 'status' => 1, 'field_name' => 0, ]); | |
$checklist->field_papka = ['target_id' => 1275]; // set taxonomy | |
$checklist->field_task->value = $value; | |
$checklist->field_task_files->appendItem($file); // append object | |
$checklist->field_papka = []; // empty term value | |
# att | |
->id(); | |
->getTitle(); | |
->getOwnerId(); - node author id | |
->changed->value; - update time | |
->field_name->getValue(); - get field value | |
->field_name->getString(); | |
# ======== paragparh ========== | |
#load by id | |
$p = \Drupal\paragraphs\Entity\Paragraph::load( $id ); | |
#load by parametrs | |
$ps = \Drupal::entityTypeManager()->getStorage('paragraph') | |
->loadByProperties(['field_name' => $value]); | |
# create | |
$paragraph = Paragraph::create([ | |
'type' => 'name', | |
'field_name' => [ | |
'value' => $value | |
] | |
]); | |
$paragraph->save(); | |
# att | |
->getParentEntity(); | |
# edit field | |
$paragraph->field_task_files->value = $value; | |
$paragraph->field_task_files->appendItem($file); | |
$paragraph->save(); | |
# ======== term ========== | |
# create Term | |
$term = \Drupal\taxonomy\Entity\Term::create([ | |
'name' => 'name', | |
'vid' => 'region', | |
'parent' => ['target_id' => $parent_id] | |
])->save(); | |
# if term | |
if($term instanceof \Drupal\taxonomy\TermInterface) | |
# get tree | |
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term') | |
->loadTree('name_of_voc', 0, 1, true); | |
->id(); | |
->getName(); | |
# ======= file ============ | |
$file = \Drupal\file\Entity\File::load($fid); | |
$file = \Drupal::entityTypeManager()->getStorage('file')->load($files[0]['target_id']); | |
$file = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['filename' => $file_name]); | |
$file->get('fid')->value; | |
$file->getFilename() | |
$file->get('uri')->value; | |
# ======= url ============== | |
use Drupal\Core\Url; | |
$url = Url::fromUri('internal:/node', $options); | |
$url = Url::fromUri('http://www.thecarneyeffect.co.uk/'); | |
$url = Url::fromRoute('entity.node.canonical', ['node' => 1]); | |
# ======== link ============= | |
use Drupal\Core\Url; | |
$options = array( | |
'query' => ['type' => 'article', 'status' => 1], | |
'fragment' => 'article-list', | |
'attributes' => ['class' => ['btn', 'btn-mini']], | |
'absolute' => TRUE, | |
); | |
$link = Link::fromTextAndUrl('title', Url::fromUri('internal:/node', $options))->toString(); | |
# ==== theming ========= | |
# in /web/themes/focus/focus.theme | |
function theme_preprocess_field(&$variables, $hook) { | |
$element = $variables['element']; | |
if (isset($element['#field_name'])) { | |
if ($element['#field_name'] == 'field_galereya_foto') { | |
$variables['attributes']['class'][] = 'image-gallery-1'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment