Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By zorax on
Hi,
I create a module for generating a custom field in a view.
It works but the $phrase generate is not html because the sentence is contained in quotation marks.
<div>
<span>
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_field' -->
<!-- BEGIN OUTPUT from 'core/themes/stable/templates/views/views-view-field.html.twig' -->
"
<div class="phrase">1 appartement neuf <br><img alt="Sigle Habitec" src="/themes/contrib/almede/images/cigle.png"><br><div class="prix">167 000 €</div></div>
"
<!-- END OUTPUT from 'core/themes/stable/templates/views/views-view-field.html.twig' -->
</span>
</div>
How to remove these quotes?
<?php
namespace Drupal\views_custom_field\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Random;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
/**
* A handler to provide a field that is completely custom by the administrator.
*
* @ingroup views_field_handlers
*
* @ViewsField("_custom_views_field")
*/
class _customViewsField extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function usesGroupBy() {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function query() {
// Do nothing -- to override the parent query.
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['hide_alter_empty'] = ['default' => FALSE];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
// Return a random text, here you can include your custom logic.
// Include any namespace required to call the method required to generate
// the desired output.
//$random = new Random();
//return $random->name();
if ($this->view->current_display=='block_offre' || $this->view->current_display=='block_residence' || $this->view->current_display=='block_terrains') {
//on load le noeud
$node = $values->_entity;
//on calcul le nombre de lots
$nb = 0;
$prix = [];
foreach ($node->field_lots as $lot) {
//dpm($lot->target_id);
if ($prix) {
$prix_leplus_bas = min($prix);
if ($nb==1) {
$phrase ='<div class="phrase">'.$nb.' appartement neuf '.'<br>'.'<img alt="Sigle Habitec" src="/themes/contrib/almede/images/cigle.png">'.'<br>'.'<div class="prix">'.$prix_leplus_bas.' €'.'</div>'.'</div>';
}
break;
if ($nb > 0 && isset($phrase)){
return $phrase ;
}
}
}
}//render
}//class
Comments
find a solution here
find a solution here
https://www.drupal.org/project/views_php/issues/2649494#comment-12004458
www.almedestudio.com