/** * {@inheritdoc} */ protected function alterViewTemplateAfterCreation(array &$view_template, $options = NULL) { parent::alterViewTemplateAfterCreation($view_template, $options); $field_defs = $this->field_manager->getBaseFieldDefinitions($this->getDefinitionValue('entity_type')); if (empty($field_defs['status'])) { // If entity doesn't have a base field status remove it from View filter. unset($view_template['display']['default']['display_options']['filters']['status']); } $this->field_manager->getFieldDefinitions($this->getDefinitionValue('entity_type'), 'event'); $this->field_manager->getFieldStorageDefinitions('node'); }
/** * {@inheritdoc} */ public function getReferencingFieldIds() { if (!isset($this->field_ids)) { $this->field_ids = []; $entity_reference_fields = $this->fieldManager->getFieldMapByFieldType('entity_reference'); $update_entity_type = $this->updateEntityType(); if (empty($entity_reference_fields[$update_entity_type])) { return $this->field_ids; } $entity_reference_fields = $entity_reference_fields[$update_entity_type]; foreach ($entity_reference_fields as $field_id => $entity_reference_field) { foreach ($entity_reference_field['bundles'] as $bundle) { $field = $this->fieldManager->getFieldDefinitions($update_entity_type, $bundle)[$field_id]; if ($field instanceof FieldConfig) { if ($field->getSetting('target_type') == 'scheduled_update' && !empty($field->getSetting('handler_settings')['target_bundles']) && in_array($this->configuration['updater_type'], $field->getSetting('handler_settings')['target_bundles']) ) { $this->field_ids[] = $field_id; } } } } } return $this->field_ids; }
/** * Gets the field definition. * * @return \Drupal\Core\Field\FieldDefinitionInterface */ protected function getFieldDefinition() { if (empty($this->fieldDefinition)) { $field_map = $this->entityFieldManager->getFieldMap(); $bundle = reset($field_map[$this->entityTypeId][$this->fieldName]['bundles']); $field_definitions = $this->entityFieldManager->getFieldDefinitions($this->entityTypeId, $bundle); $this->fieldDefinition = $field_definitions[$this->fieldName]; } return $this->fieldDefinition; }
public function getMainDiagram() { $entity_definitions = $this->entityTypeManager->getDefinitions(); $entities = []; $links = []; foreach ($entity_definitions as $definition) { $entity = ['id' => $definition->id(), 'type' => 'type', 'type_label' => t('Entity Type'), 'label' => $definition->getLabel(), 'provider' => $definition->getProvider(), 'group' => $definition->getGroup(), 'bundles' => []]; if ($definition instanceof ConfigEntityTypeInterface) { $entity['config_properties'] = $definition->getPropertiesToExport(); } $bundles = $this->entityTypeBundleInfo->getBundleInfo($definition->id()); foreach ($bundles as $bundle_id => $bundle_label) { $bundle = ['id' => $bundle_id, 'type' => 'bundle', 'type_label' => t('Entity Bundle'), 'label' => $bundle_label['label'], 'entity_type' => $definition->id()]; if ($definition->isSubclassOf(FieldableEntityInterface::class)) { $bundle['fields'] = []; $fields = $this->entityFieldManager->getFieldDefinitions($definition->id(), $bundle_id); foreach ($fields as $field) { $field_storage_definition = $field->getFieldStorageDefinition(); $field_settings = $field->getItemDefinition()->getSettings(); $field_name = $field_storage_definition->getName(); $bundle['fields'][$field_name] = ['id' => $field_name, 'label' => $field->getLabel(), 'type' => $field_storage_definition->getType(), 'description' => $field_storage_definition->getDescription(), 'cardinality' => $field_storage_definition->getCardinality(), 'is_multiple' => $field_storage_definition->isMultiple()]; $types[$field_storage_definition->getType()] = $field_storage_definition->getType(); if ($bundle['fields'][$field_name]['type'] == 'entity_reference') { $link = ['label' => t('Entity Reference from field "@field_name"', ['@field_name' => $field_name]), 'from' => 'bundle:' . $bundle_id, 'from_selector' => '.attribute-background-' . $field_name, 'targets' => ['type:' . $field_settings['target_type']]]; if (isset($field_settings['handler_settings']['target_bundles'])) { foreach ($field_settings['handler_settings']['target_bundles'] as $target_bundle) { $link['targets'][] = 'bundle:' . $target_bundle; } } $links[] = $link; } else { if ($bundle['fields'][$field_name]['type'] == 'image') { $links[] = ['label' => t('Image Reference from field "@field_name"', ['@field_name' => $field_name]), 'from' => 'bundle:' . $bundle_id, 'from_selector' => '.attribute-background-' . $field_name, 'targets' => ['type:' . $field_settings['target_type']]]; } } } } $entity['bundles'][$bundle_id] = $bundle; } $entities[$definition->id()] = $entity; } return ['#markup' => '<div class="erd-actions">' . '<i title="Add Entity Type or Bundle" class="erd-search">' . ' <input type="text"/>' . '</i>' . '<i title="Add editable label" class="erd-label"></i>' . '<i title="Change link styles" class="erd-line-style"></i>' . '<i title="Toggle machine names" class="erd-machine-name"></i>' . '<i title="Save to image" class="erd-save"></i>' . '<i title="Zoom in" class="erd-zoom"></i>' . '<i title="Zoom out" class="erd-unzoom"></i>' . '</div>' . '<div class="erd-container"></div>', '#allowed_tags' => ['input', 'div', 'i'], '#attached' => ['library' => ['erd/main'], 'drupalSettings' => ['erd' => ['entities' => $entities, 'links' => $links]]]]; }
/** * Helper function to get the field definitions. * * @param string $entityTypeId * The entity type we want to inspect. * @param string $bundle * The bundle we want to discover the fields of. * @return array * An array of field storage definitions for the entity type, * keyed by field name. */ private function getBundleFields($entityTypeId, $bundle) { $fields = []; if (!empty($entityTypeId) && !empty($bundle)) { $fields = array_filter($this->entityFieldManager->getFieldDefinitions($entityTypeId, $bundle), function ($fieldDefinition) { return $fieldDefinition instanceof FieldConfigInterface; }); } return $fields; }
/** * Gets a list of the file fields of the entity this field is attached to. * * @return array * An array of fields keyed by their id. */ public function getAvailableFields() { $available_fields = []; /** @var FieldDefinitionInterface $field_definition */ foreach ($this->entityFieldManager->getFieldDefinitions($this->targetEntityTypeId, $this->targetBundle) as $field_key => $field_definition) { if ($field_definition->getType() == 'file') { $available_fields[$field_key] = $field_definition->label(); } } return $available_fields; }
/** * {@inheritdoc} */ public function build(FacetInterface $facet, array $results) { $field_identifier = $facet->getFieldIdentifier(); $entity = 'node'; // Support multiple entities when using Search API. if ($facet->getFacetSource() instanceof SearchApiFacetSourceInterface) { $index = $facet->getFacetSource()->getIndex(); $field = $index->getField($field_identifier); $entity = str_replace('entity:', '', $field->getDatasourceId()); } // If it's an entity base field, we find it in the field definitions. // We don't have access to the bundle via SearchApiFacetSourceInterface, so // we check the entity's base fields only. $base_fields = $this->entityFieldManager->getFieldDefinitions($entity, ''); // This only works for configurable fields. $config_entity_name = sprintf('field.storage.%s.%s', $entity, $field_identifier); if (isset($base_fields[$field_identifier])) { $field = $base_fields[$field_identifier]; } elseif ($this->configManager->loadConfigEntityByName($config_entity_name) !== NULL) { $field = $this->configManager->loadConfigEntityByName($config_entity_name); } if ($field) { $function = $field->getSetting('allowed_values_function'); if (empty($function)) { $allowed_values = $field->getSetting('allowed_values'); } else { $allowed_values = ${$function}($field); } if (is_array($allowed_values)) { /** @var \Drupal\facets\Result\ResultInterface $result */ foreach ($results as &$result) { if (isset($allowed_values[$result->getRawValue()])) { $result->setDisplayValue($allowed_values[$result->getRawValue()]); } } } } return $results; }
/** * {@inheritdoc} */ public function getAllFieldConfigsForField(FieldStorageDefinitionInterface $definition, $entity_type_id) { $map = $this->entityFieldManager->getFieldMap()[$entity_type_id]; $definitions = []; $field_name = $definition->getName(); if (isset($map[$field_name])) { $bundles = $map[$field_name]['bundles']; foreach ($bundles as $bundle) { $bundle_definitions = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle); $definitions[$bundle] = $bundle_definitions[$field_name]; } } return $definitions; }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { /** @var \Drupal\contact\ContactFormInterface $contact_form */ $contact_form = $this->entity; // Get the original ID. $original_id = $contact_form->getOriginalId(); $new_id = $contact_form->id(); // Create the new form. $contact_form = $contact_form->createDuplicate(); $contact_form->set('id', $new_id); $contact_form->save(); // Clone configurable fields. foreach ($this->fieldManager->getFieldDefinitions('contact_message', $original_id) as $field) { if ($field instanceof BaseFieldDefinition) { continue; } if ($this->moduleHandler->moduleExists('field')) { if ($config = $field->getConfig($original_id)) { $new_config = FieldConfig::create([ 'bundle' => $contact_form->id(), 'uuid' => NULL, ] + $config->toArray()); $new_config->save(); } } } // Clone the entity form display. $display = EntityFormDisplay::load('contact_message.' . $original_id . '.default'); EntityFormDisplay::create([ 'bundle' => $contact_form->id(), 'uuid' => NULL, ] + $display->toArray())->save(); // Clone the entity view display. $display = EntityViewDisplay::load('contact_message.' . $original_id . '.default'); EntityViewDisplay::create([ 'bundle' => $contact_form->id(), 'uuid' => NULL, ] + $display->toArray())->save(); // Redirect and show messge. $form_state->setRedirect('entity.contact_form.edit_form', ['contact_form' => $contact_form->id()]); $edit_link = $this->entity->link($this->t('Edit')); drupal_set_message($this->t('Contact form %label has been added.', array('%label' => $contact_form->label()))); $this->logger('contact')->notice('Contact form %label has been added.', array('%label' => $contact_form->label(), 'link' => $edit_link)); }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { $status = $this->entity->save(); // Update the default value of the status field. $product = $this->entityTypeManager->getStorage('commerce_product')->create(['type' => $this->entity->id()]); $value = (bool) $form_state->getValue('product_status'); if ($product->status->value != $value) { $fields = $this->entityFieldManager->getFieldDefinitions('commerce_product', $this->entity->id()); $fields['status']->getConfig($this->entity->id())->setDefaultValue($value)->save(); $this->entityFieldManager->clearCachedFieldDefinitions(); } drupal_set_message($this->t('The product type %label has been successfully saved.', ['%label' => $this->entity->label()])); $form_state->setRedirect('entity.commerce_product_type.collection'); if ($status == SAVED_NEW) { commerce_product_add_stores_field($this->entity); commerce_product_add_body_field($this->entity); commerce_product_add_variations_field($this->entity); } }
/** * Helper function: Query to get the list of flippy pagers. * * @parameter * current node object * * @return array * a list of flippy pagers */ public function flippy_build_list($node) { // Get all the properties from the current node. $master_list =& drupal_static(__FUNCTION__); if (!isset($master_list)) { $master_list = array(); } if (!isset($master_list[$node->id()])) { // Check to see if we need custom sorting if ($this->flippySettings->get('flippy_custom_sorting_' . $node->getType())) { // Get order $order = $this->flippySettings->get('flippy_order_' . $node->getType()); // Get sort $sort = $this->flippySettings->get('flippy_sort_' . $node->getType()); } else { $order = 'ASC'; $sort = 'created'; } // Validate that the sort criteria is OK to use. // Achieve the base field from a node type. $sort_options = array(); // Get all the field from a node type. $content_type_fields = $this->entityFieldManager->getFieldDefinitions('node', $node->getType()); foreach ($content_type_fields as $sort_field) { if (get_class($sort_field) == 'Drupal\\Core\\Field\\BaseFieldDefinition') { // It is a base field. $schema_info = $sort_field->getSchema(); } if (isset($schema_info['columns']['value']) && $schema_info['columns']['value']['type'] == 'int') { $sort_options[] = $sort_field->getName(); } } $base_table_properties = $sort_options; $field_value = NULL; // If the sort criteria is not in the $sort_option array, we assume it's // a field. if (!in_array($sort, $base_table_properties)) { // Get the value of the current node's field (use the first one only) $current_field_items = $node->{$sort}->getValue(); if (!isset($current_field_items[0]['value'])) { // Should never happen, but just in case, fall back to post date // ascending. $sort = 'created'; $order = 'ASC'; } else { // Otherwise save the field value for later. $field_value = $current_field_items[0]['value']; } } // Depending on order, decide what before and after means. $before = $order == 'ASC' ? '<' : '>'; $after = $order == 'ASC' ? '>' : '<'; // Also decide what up and down means $up = $order == 'ASC' ? 'ASC' : 'DESC'; $down = $order == 'ASC' ? 'DESC' : 'ASC'; // Create a starting-point EntityQuery object. $query = $this->nodeQuery; $query->condition('type', $node->getType())->condition('status', 1)->condition('nid', $node->id(), '!=')->addTag('node_access'); // Create the individual queries $first = clone $query; $prev = clone $query; $next = clone $query; $last = clone $query; $random = clone $query; // We will construct the queries differently depending on whether the // sorting criteria is a field or a base table property. // If we found a field value earlier, we know we're dealing with a field if (isset($field_value)) { // Set the conditions // The first query. $field_value_equal_condition = $first->orConditionGroup()->condition($sort . '.value', $field_value, '=')->condition($sort . '.value', NULL, 'IS NULL'); $field_default_condition = $first->andConditionGroup()->condition('nid', $node->id(), $before)->condition($field_value_equal_condition); $field_sorting_group = $first->orConditionGroup()->condition($sort . '.value', $field_value, $before)->condition($field_default_condition); $first->condition($field_sorting_group); // The last query. $field_value_equal_condition = $last->orConditionGroup()->condition($sort . '.value', $field_value, '=')->condition($sort . '.value', NULL, 'IS NULL'); $field_default_condition = $last->andConditionGroup()->condition('nid', $node->id(), $after)->condition($field_value_equal_condition); $field_sorting_group = $last->orConditionGroup()->condition($sort . '.value', $field_value, $after)->condition($field_default_condition); $last->condition($field_sorting_group); // Previous query to find out the previous item based on the field, using // node id if the other criteria is the same. // The prev query. $field_value_equal_condition = $prev->orConditionGroup()->condition($sort . '.value', $field_value, '=')->condition($sort . '.value', NULL, 'IS NULL'); $field_default_condition = $prev->andConditionGroup()->condition('nid', $node->id(), $before)->condition($field_value_equal_condition); $field_sorting_group = $prev->orConditionGroup()->condition($sort . '.value', $field_value, $before)->condition($field_default_condition); $prev->condition($field_sorting_group); // The next query. $field_value_equal_condition = $next->orConditionGroup()->condition($sort . '.value', $field_value, '=')->condition($sort . '.value', NULL, 'IS NULL'); $field_default_condition = $next->andConditionGroup()->condition('nid', $node->id(), $after)->condition($field_value_equal_condition); $field_sorting_group = $next->orConditionGroup()->condition($sort . '.value', $field_value, $after)->condition($field_default_condition); $next->condition($field_sorting_group); // set the ordering $first->sort($sort, $up); $prev->sort($sort, $down); $next->sort($sort, $up); $last->sort($sort, $down); } else { // Otherwise we assume the variable is a column in the base table // (a property). Like above, set the conditions. $sort_value = $node->get($sort); $sort_value = $sort_value->getValue(); // First and last query. $first->condition($sort, $sort_value[0]['value'], $before); $last->condition($sort, $sort_value[0]['value'], $after); // Previous query to find out the previous item based on the field, // using node id if the other criteria is the same. $field_default_condition = $prev->andConditionGroup()->condition($sort, $sort_value[0]['value'])->condition('nid', $node->id(), $before); $field_sorting_group = $prev->orConditionGroup()->condition($sort, $sort_value[0]['value'], $before)->condition($field_default_condition); $prev->condition($field_sorting_group); // Next query to find out the next item based on the field, using // node id if the other criteria is the same. $field_default_condition = $next->andConditionGroup()->condition($sort, $sort_value[0]['value'])->condition('nid', $node->id(), $after); $field_sorting_group = $next->orConditionGroup()->condition($sort, $sort_value[0]['value'], $after)->condition($field_default_condition); $next->condition($field_sorting_group); // Set the ordering. $first->sort($sort, $up); $prev->sort($sort, $down); $next->sort($sort, $up); $last->sort($sort, $down); } // Event dispatcher. $queries = ['first' => $first, 'prev' => $prev, 'next' => $next, 'last' => $last]; $event = new FlippyEvent($queries); $this->eventDispatcher->dispatch('buildFlippyQuery', $event); $queries = $event->getQueries(); // Execute the queries. $results = array(); $results['first'] = $queries['first']->range(0, 1)->execute(); $results['first'] = !empty($results['first']) ? array_values($results['first'])[0] : NULL; $results['prev'] = $queries['prev']->range(0, 1)->execute(); $results['prev'] = !empty($results['prev']) ? array_values($results['prev'])[0] : NULL; $results['next'] = $queries['next']->range(0, 1)->execute(); $results['next'] = !empty($results['next']) ? array_values($results['next'])[0] : NULL; $results['last'] = $queries['last']->range(0, 1)->execute(); $results['last'] = !empty($results['last']) ? array_values($results['last'])[0] : NULL; $node_ids = array(); foreach ($results as $key => $result) { // If the query returned no results, it means we're already // at the beginning/end of the pager, so ignore those. if (count($result) > 0) { // Otherwise we save the node ID. $node_ids[$key] = $results[$key]; } } // Make our final array of node IDs and titles. $list = array(); // but only if we actually found some matches if (count($node_ids) > 0) { // We also need titles to go with our node ids. $title_query = $this->connection->select('node_field_data', 'nfd')->fields('nfd', array('title', 'nid'))->condition('nfd.nid', $node_ids, 'IN')->execute()->fetchAllAssoc('nid'); foreach ($node_ids as $key => $nid) { $list[$key] = array('nid' => $nid, 'title' => isset($title_query[$nid]) ? $title_query[$nid]->title : ''); } } // Create random list, // todo: orderRandom is not available in entityQuery yet. if ($this->flippySettings->get('flippy_random_' . $node->getType())) { $random_nids = $random->execute(); $random_nid = array_rand($random_nids, 1); // Find out the node title. $title = $this->connection->select('node_field_data', 'nfd')->fields('nfd', array('title'))->condition('nfd.nid', $random_nid, '=')->execute()->fetchField(); $list['random'] = array('nid' => $random_nid, 'title' => $title); } // Finally set the current info for themers to use. $list['current'] = array('nid' => $node->id(), 'title' => $node->getTitle()); $master_list[$node->id()] = $list; } return $master_list[$node->id()]; }