Skip to content

Instantly share code, notes, and snippets.

View ozin7's full-sized avatar
🎯
Focusing

Mykhailo Hurei ozin7

🎯
Focusing
  • Lviv, Ukraine
  • 23:52 (UTC +03:00)
View GitHub Profile
@ozin7
ozin7 / ExampleApiClient.php
Last active August 11, 2022 16:28
ExampleApiClient example
<?php
namespace Drupal\api_client_example\Service;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Site\Settings;
use GuzzleHttp\ClientInterface;
/**
* Class ApiClientInterface.
@ozin7
ozin7 / update.php
Last active May 8, 2021 07:07
Drupal 8/9: Update entity path alias
<?php
use Drupal\pathauto\PathautoState;
$node->set('path', ['alias' => $extra_data['URL'], 'pathauto' => PathautoState::SKIP]);
@ozin7
ozin7 / table.php
Last active August 11, 2022 16:30
Drupal 8/9: Add sorting and pager to the Database connection query
<?php
use Drupal\Core\Database\Query\PagerSelectExtender;
use Drupal\Core\Database\Query\TableSortExtender;
// Field = column name in your table.
$header = [
'status' => ['data' => $this->t('Order status'), 'field' => 'status'],
'order_id' => ['data' => $this->t('Order number'), 'field' => 'order_id', 'sort' => 'desc'],
'placed' => ['data' => $this->t('Order date'), 'field' => 'placed'],
'seller' => ['data' => $this->t('Seller'), 'field' => 'seller'],
@ozin7
ozin7 / example.php
Last active July 10, 2020 13:03
Drupal 9: Sort structured array by the 'weight', 'title', '#weight', '#title'
<?php
use Drupal\Component\Utility\SortArray;
// Sort by `weight`
$exapmle_array = [
['nid' => 21, 'weight' => -10],
['nid' => 3, 'weight' => 10],
['nid' => 1, 'weight' => -100],
];
uasort($data, [SortArray::class, 'sortByWeightElement']);
@ozin7
ozin7 / CategoriesSuggestionSettingsForm.php
Last active August 11, 2022 16:32
Drupal 8/9: Form with "Add more" ajax button
<?php
namespace Drupal\gurei\Form;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
@ozin7
ozin7 / entityListBuilder.php
Last active April 18, 2020 12:32
Drupal 8: Table drag
<?php
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
if ($this->entities) {
/** @var \Drupal\commerce_promotion\Entity\Promotion $promotion */
foreach ($this->entities as $promotion) {
$form['promotions'][$promotion->id()]['name'] = [
'name' => $form['promotions'][$promotion->id()]['name'],
];
$has_parent = FALSE;
@ozin7
ozin7 / QueryFactory.php
Last active August 11, 2022 16:38
Drupal 8: Override service methods(service decorator)
<?php
/**
* Workspaces-specific entity query implementation.
*/
class QueryFactory extends BaseQueryFactory {
/**
* {@inheritdoc}
*/
@ozin7
ozin7 / link.php
Last active August 11, 2022 16:39
Drupal 8/9: Set active class for the custom link
@ozin7
ozin7 / migrate.yml
Last active March 28, 2020 13:46
Drupal 8: Skip updating some entity fields during migration
destination:
plugin: entity:user
# If the destination is going to update an existing user, you can optionally
# specify the properties that should be overwritten. For example, if the
# migration tries to import user 31 and user 31 already exists in the
# destination database, only the 'name' and 'mail' properties of the user
# will be overwritten. If user 31 doesn't exist, it will be created and
# the overwrite_properties list will be ignored.
overwrite_properties:
- name
@ozin7
ozin7 / CompanyService.php
Last active August 11, 2022 16:40
Drupal 9: String translation in a Service
<?php
namespace Drupal\gurei\Service;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
/**
* Class CompanyService.
*/
class CompanyService {