public static function saveExpo_exist($d, $nid) { $node = node_load($nid); $node->setTitle($d['title']); $node->field_authoremail->setValue($d['email']); $node->field_author_plain->setValue($d['author']); $node->field_public->setValue($d['public']); //tag field_tag Entity reference if (is_array($d['body'])) { $body_value = ['format' => 'rich_html_base', 'value' => $d['body']['value']]; } else { $body_value = ['format' => 'rich_html_base', 'value' => $d['body']]; } //if(is_array($d['body'])) $body_value = $d['body']['value']; //else $body_value = $d['body']; $node->field_body->setValue($body_value); if ($d['featuredimage']) { if (is_array($d['featuredimage'])) { $target_id = $d['featuredimage']['target_id']; if ($node->field_featuredimage) { $node->field_featuredimage->setValue(['target_id' => $target_id]); } } } $node->save(); $fcbundle = 'field_collitem'; $cnt = count($node->{$fcbundle}); for ($i = 0; $i < $cnt; $i++) { $fcid = $node->field_collitem[$i]->value; $fcitem = \Drupal\field_collection\Entity\FieldCollectionItem::load($fcid); $fcitem->delete(); } $node->{$fcbundle} = []; //foreach ($node->{$fcbundle} as $key => $value) { // unset($node->{$fcbundle}[$key]); //} //$node->save(); foreach ($d['collitems'] as $item) { $collitem = ['field_name' => 'field_collitem']; $collitem['field_target'] = array('value' => $item['target']); if ($item['annotation']) { $collitem['field_annotation'] = $item['annotation']; } $fcitem = FieldCollectionItem::create($collitem); $fcitem->setHostEntity($node, false); $node->{$fcitem->bundle()}[] = array('field_collection_item' => $fcitem); $fcitem->save(true); } $node->save(); return $node; }
private function content_ajax_page_fc($fcid) { $fcitem = \Drupal\field_collection\Entity\FieldCollectionItem::load($fcid); $target = WG::entity_get_field_value($fcitem, 'field_target'); preg_match('%^([^:]+)://([0-9]+)$%', $target, $m); $itemtype = $m[1]; $id = $m[2]; switch ($itemtype) { case 'public318': $identifier = $id; $text = WG::entity_get_field_formatted_text($fcitem, 'field_annotation'); $stylename = 'large'; $icon_uri = _expo_public318_get_icon_uri($identifier); $icontag = WG::render_styled_image($icon_uri, $stylename); $output = '<div class="sticky-fc-public318">' . '<div class="collicon">' . _expo_coll_url($identifier, $icontag) . '</div>' . '<div class="colltext">' . $text . '</div>' . '</div>'; break; case 'storynode': $nid = $id; $story = node_load($nid); $v = entity_view($story, 'ajaxpage'); $output = render($v); break; default: $tag = "<div class=\"sticky\" id=\"sticky_{$pos}\">" . $itemtype . $pos . "</div>"; } $build = ['#markup' => $output]; return $build; }
public function content($nid) { $node = \Drupal::entityManager()->getStorage('node')->load($nid); $fields = $node->getFieldDefinitions(); $fcArray = []; foreach ($fields as $field){ $type = $field->getType(); if ($type == 'field_collection') { $field_collection = $field->get('field_name'); foreach ($node->$field_collection as $key => $item){ $item = $item->value; // $fcArray[$key]; $fc = FieldCollectionItem::load($item); foreach ($fc as $fckey => $field){ $fcArray[$key][$fckey] = $field->value; if (is_object($field[0]) && $field[0]->height){ $image = File::load($field[0]->target_id); $fcArray[$key][$fckey] = str_replace('public://', '/drupal/sites/default/files/', $image->uri->value); $fcArray[$key]['field_mosaic_image_alt'] = $field[0]->alt; } if (is_object($field[0]) && $field[0]->uri){ $fcArray[$key][$fckey] = str_replace('internal:', '', $field[0]->uri); } } } } } return new JsonResponse($fcArray); }
public function getFieldCollectionItem($create = FALSE) { if (isset($this->field_collection_item)) { return $this->field_collection_item; } elseif (isset($this->value)) { // By default always load the default revision, so caches get used. $field_collection_item = FieldCollectionItem::load($this->value); if ($field_collection_item !== NULL && $field_collection_item->getRevisionId() != $this->revision_id) { // A non-default revision is a referenced, so load this one. $field_collection_item = \Drupal::entityTypeManager()->getStorage('field_collection_item')->loadRevision($this->revision_id); } return $field_collection_item; } elseif ($create) { $field_collection_item = FieldCollectionItem::create(['field_name' => $this->getFieldDefinition()->getName()]); // TODO: Uncomment or delete /* $field_collection_item->setHostEntity($this->getEntity(), FALSE); */ return $field_collection_item; } return FALSE; }
/** * Make sure the basic UI and access checks are working. */ public function testBasicUI() { $node = $this->drupalCreateNode(array('type' => 'article')); // Login with new user that has no privileges. $user = $this->drupalCreateUser(array('access content')); $this->drupalLogin($user); // Make sure access is denied. $path = "field_collection_item/add/field_test_collection/node/{$node->id()}"; $this->drupalGet($path); $this->assertText(t('Access denied'), 'Access has been denied.'); // Login with new user that has basic edit rights. $user_privileged = $this->drupalCreateUser(['access content', 'edit any article content']); $this->drupalLogin($user_privileged); // Test field collection item add form. $this->drupalGet('admin/structure/types/manage/article/display'); $this->drupalGet("node/{$node->id()}"); $this->assertLinkByHref($path, 0, 'Add link is shown.'); $this->drupalGet($path); $this->assertText(t($this->inner_field_definition['label'])); $edit = array("{$this->inner_field_name}[0][value]" => rand()); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText(t('Successfully added a @field.', array('@field' => $this->field_collection_name))); $this->assertText($edit["{$this->inner_field_name}[0][value]"]); $field_collection_item = FieldCollectionItem::load(1); // Test field collection item edit form. $edit["{$this->inner_field_name}[0][value]"] = rand(); $this->drupalPostForm('field_collection_item/1/edit', $edit, t('Save')); $this->assertText(t('Successfully edited @field.', array('@field' => $field_collection_item->label()))); $this->assertText($edit["{$this->inner_field_name}[0][value]"]); $this->drupalGet('field_collection_item/1'); $this->assertText($edit["{$this->inner_field_name}[0][value]"]); }
public function content_public318edit($id) { $entity = node_load($id); $r = ''; if ($entity) { $form = ['items' => array()]; $cnt = count($entity->field_collitem); for ($i = 0; $i < $cnt; $i++) { $fcid = $entity->field_collitem[$i]->value; $fcitem = \Drupal\field_collection\Entity\FieldCollectionItem::load($fcid); $target = WG::entity_get_field_value($fcitem, 'field_target'); $annotation = WG::entity_get_field_value($fcitem, 'field_annotation'); extract(_expo_extract_collitem_target($target)); $identifier = $id; //$collitems[] = ['target'=>$target, 'annotation'=>$annotation]; $delta = microtime(); $delta = preg_replace('%[^0-9]%', '', $delta); $form['items'][$delta] = ['#type' => 'container', '#prefix' => '<li><div class="collitem-element-wrapper" id="collitem-index-' . $delta . '">', '#suffix' => '</div></li>']; $this->_collitem_element_make2($form['items'][$delta], $delta, $identifier, $fcid, $annotation); } $r .= render($form); } echo $r; die; }