Load menu item from node
use Drupal\menu_link_content\Entity\MenuLinkContent;
$node_id = 1;
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$result = $menu_link_manager->loadLinksByRoute('entity.node.canonical', array('node' => $node_id));
dpm($result);
Get Node from request
$this->node = \Drupal::routeMatch()->getParameter('node');
Get URL
\Drupal::request()->getRequestUri();
... better
$current_path = \Drupal::service('path.current')->getPath();
$result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
_Get nodes url_
$node->toUrl()->toString();
_Get Node ID from alias_
$node_path = \Drupal::service('path.alias_storage')->load(array(
'alias' => $node_alias
));
Config Management
http://nuvole.org/blog/2014/aug/20/git-workflow-managing-drupal-8-configuration
config-export -> git commit -> git pull -> (fix any conflicts) -> config-import -> (if no prob) -> git push
Config API
$config = $this->config('dnsw_middleware_connect.taxonomies');
$this->product_attribute_types = $config->get('taxonomies.product_attribute_types.vid');
or...
$language_config = \Drupal::config('language.negotiation');
$language_domains = $language_config->get('url.domains');
Null Cache Backend
Add to development.services.yml:
services:
cache.backend.null:
class: Drupal\Core\Cache\NullBackendFactory
Hit ~> mystupid.site/core/rebuild.php
Add to settings.local.php:
//cache stuff
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
Render an entity
$field_output = entity_view($node->get('field_entity_reference')->entity, 'default');
Entity Storage
$entityTypeManager = \Drupal::service('entity_type.manager');
$nodeStorage = $entityTypeManager->getStorage('node')
Query Entity
$nodeStorage->getQuery()->condition('bundle_type', 'page')->execute()
$query = \Drupal::entityQuery('taxonomy_term');
$query->condition('vid', DERP_VID)
->condition('field_derp', $derp_value)
->range(0, 1)
;
// bypass access control to eg get unpublished nodes
$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'your_content_type')
->accessCheck(FALSE);
// Easier alternative
$users = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties([
'name' => 'bar'
]);
_Load entity reference_
// $id = some node ID
// $field = field name for entity reference field
$node = Node::load($id);
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $referenceItem */
$referenceItem = $node->get($field)->first();
/** @var \Drupal\Core\Entity\Plugin\DataType\EntityReference $entityReference */
$entityReference = $referenceItem->get('entity');
/** @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter $entityAdapter */
$entityAdapter = $entityReference->getTarget();
/** @var \Drupal\Core\Entity\EntityInterface $referencedEntity */
$referencedEntity = $entityAdapter->getValue();
// At this point $referencedEntity is the referenced entity object.
_easier alternative_
$entities = $node->field_ent_ref->referencedEntities();
Load a Term
Term::load($term_id);
Logging/Watchdog
\Drupal::logger('broken_arse_module')->error('ye code be fucked');
Autocomplete Textfield
$form['derp'] = [
'#title' => 'Derp',
'#type' => 'textfield',
'#autocomplete_route_name' => 'my.router',
];
router returns:
return new JsonResponse([
['value' => 'josh', 'label' => 'jsoh!'],
['value' => 'was', 'label' => 'here']
]);
get the frontpage/homepage/front page/home page node
if (\Drupal::service('path.matcher')->isFrontPage()) {
$site_config = \Drupal::config('system.site');
$frontpage_path = $site_config->get('page.front');
$nid = substr($frontpage_path, 6);
service
https://api.drupal.org/api/drupal/services
Load Node
$entity_manager = \Drupal::entityTypeManager();
$node = $entity_manager->getStorage('node')->load($nid);
404
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
...
throw new NotFoundHttpException();
to debug drush need to set XDEBUG key in environment vars:
export XDEBUG_CONFIG="idekey=PHPSTORM"
get latest db from acquia and restore to local
ACQUIA_DB='target_db' VAGRANT_ALIAS='@localvm.site' \
URL=`drush @acquia_remote.prod ac-database-instance-backup-list ${ACQUIA_DB} --format="json" | \
jq -r '.[-1]["link"]'` && \
wget ${URL} -O /tmp/${ACQUIA_DB}.sql.tar.gz && \
drush $VAGRANT_ALIAS sql-drop -y && \
zcat /tmp/${ACQUIA_DB}.sql.tar.gz | \
drush $VAGRANT_ALIAS sql-cli && \
drush $VAGRANT_ALIAS cr && \
drush $VAGRANT_ALIAS cim -y && \
drush $VAGRANT_ALIAS cr
Multiple File upload maximum file count:
php.ini setting:
max_file_uploads = 20
clear ip ban hammer
> truncate flood;
Unblock cron
DELETE FROM semaphore WHERE name='cron';
xhprof
https://github.com/tideways/php-xhprof-extension
```
<?php
tideways_xhprof_enable(TIDEWAYS_XHPROF_FLAGS_MEMORY | TIDEWAYS_XHPROF_FLAGS_CPU);
my_application();
file_put_contents(
sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid() . '.myapplication.xhprof',
serialize(tideways_xhprof_disable())
);
```
Reset module schema version
```
\Drupal::service('update.update_hook_registry')->setInstalledVersion('bla_module', 8900);
```
now you can rerun updb
Composer memory
COMPOSER_MEMORY_LIMIT=-1 composer require drupal/some_module