/** * Create fields for the selected properties. */ protected function createField() { $entity_type = 'node'; $bundle = $this->entity->id(); foreach ($this->properties as $key => $value) { $label = $this->converter->label($key); // Add the field prefix and truncate if longer than 32 char. $field_name = $this->prefix . strtolower($label); if (strlen($field_name) > 32) { $field_name = substr($field_name, 0, 32); } $field_storage = array( 'field_name' => $field_name, 'entity_type' => $entity_type, 'type' => $value['type'], 'translatable' => TRUE, ); $instance = array( 'field_name' => $field_name, 'entity_type' => $entity_type, 'bundle' => $bundle, 'label' => $label, // Field translatability should be explicitly enabled by the users. 'translatable' => FALSE, ); // Create the field and instance. try { \Drupal::entityTypeManager()->getStorage('field_storage_config')->create($field_storage)->save(); \Drupal::entityTypeManager()->getStorage('field_config')->create($instance)->save(); // Make sure the field is displayed in the 'default' form mode (using // default widget and settings). It stays hidden for other form modes // until it is explicitly configured. entity_get_form_display($entity_type, $bundle, 'default') ->setComponent($field_name) ->save(); // Make sure the field is displayed in the 'default' view mode (using // default formatter and settings). It stays hidden for other view // modes until it is explicitly configured. entity_get_display($entity_type, $bundle, 'default') ->setComponent($field_name) ->save(); // RDF Mapping. $this->rdfMapping->setFieldMapping($field_name, array( 'properties' => array($key), ) ); } catch (\Exception $e) { drupal_set_message($this->t('There was a problem creating field %label: !message', array( '%label' => $instance['label'], '!message' => $e->getMessage(), )), 'error'); } } }
/** * Create the referrer entity. */ protected function createReferrerEntity() { /** @var \Drupal\node\Entity\Node $node */ $node = entity_create($this->testEntityTypeName, array('title' => $this->randomMachineName(), 'type' => $this->referrerType->id(), 'description' => array('value' => $this->randomMachineName(), 'format' => 'basic_html'), $this->referenceFieldName => array(array('target_id' => $this->referencedEntityWithTranslation->id()), array('target_id' => $this->referencedEntityWithoutTranslation->id())), 'langcode' => $this->baseLangcode)); $node->save(); $node->addTranslation($this->translateToLangcode, $node->toArray()); $node->save(); return $node; }
/** * Tests that deletions of custom bundles are mirrored in field settings. */ public function testCustomTargetBundleDeletion() { // Attach an entity reference field to $this->nodeType. $name = Unicode::strtolower($this->randomMachineName()); $label = $this->randomString(); $handler_settings = ['target_bundles' => [$this->customBundle => $this->customBundle]]; $this->createEntityReferenceField('node', $this->nodeType->id(), $name, $label, 'entity_test', 'default', $handler_settings); // Check that the 'target_bundle' setting contains the custom bundle. $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name); $actual_handler_settings = $field_config->getSetting('handler_settings'); $this->assertEqual($handler_settings, $actual_handler_settings); // Delete the custom bundle. entity_test_delete_bundle($this->customBundle, 'entity_test'); // Check that the deleted bundle is no longer present in the // 'target_bundles' field setting. $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name); $handler_settings = $field_config->getSetting('handler_settings'); $this->assertTrue(empty($handler_settings['target_bundles'])); }
/** * Builds a standard list of node permissions for a given type. * * @param \Drupal\node\Entity\NodeType $type * The machine name of the node type. * * @return array * An array of permission names and descriptions. */ protected function buildPermissions(NodeType $type) { $type_id = $type->id(); $type_params = array('%type_name' => $type->label()); return array("create {$type_id} content" => array('title' => $this->t('%type_name: Create new content', $type_params)), "edit own {$type_id} content" => array('title' => $this->t('%type_name: Edit own content', $type_params)), "edit any {$type_id} content" => array('title' => $this->t('%type_name: Edit any content', $type_params)), "delete own {$type_id} content" => array('title' => $this->t('%type_name: Delete own content', $type_params)), "delete any {$type_id} content" => array('title' => $this->t('%type_name: Delete any content', $type_params)), "view {$type_id} revisions" => array('title' => $this->t('%type_name: View revisions', $type_params)), "revert {$type_id} revisions" => array('title' => $this->t('%type_name: Revert revisions', $type_params), 'description' => t('Role requires permission <em>view revisions</em> and <em>edit rights</em> for nodes in question, or <em>administer nodes</em>.')), "delete {$type_id} revisions" => array('title' => $this->t('%type_name: Delete revisions', $type_params), 'description' => $this->t('Role requires permission to <em>view revisions</em> and <em>delete rights</em> for nodes in question, or <em>administer nodes</em>.'))); }
/** * Returns a list of view unpublished permissions for a given node type. * * @param \Drupal\node\Entity\NodeType $type * The node type. * * @return array * An associative array of permission names and descriptions. */ protected function buildPermissions(NodeType $type) { $type_id = $type->id(); $type_params = array('%type_name' => $type->label()); return array("view any unpublished {$type_id} content" => array('title' => $this->t('%type_name: View any unpublished content', $type_params))); }